Question

I need to copy folder with all it's content (files and folders) from Document Library to another Document library in another site.

The code: SPFolder.Copy(destinationUrl) works fine only when both source and destination docLibs are in the same site, but if the destination docLib is in another site I got the error: Value does not fall whithin the expected range.

Is there a simple way to do this?

thanks.

Was it helpful?

Solution

SPFolder.Copy can't be use for cross site collection per my knowledge.

Here is sample script for cross site collection.

$ver = $host | select version
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

##
#Set Static Variables
##

$SourceWebURL = "http://sp"
$SourceLibraryTitle = "Doc1"
$DestinationWebURL = "http://sp:12001"
$DestinationLibraryTitle = "Doc1"

##
#Begin Script
##

$sWeb = Get-SPWeb $SourceWebURL
$sFolder=$sWeb.GetFolder("/Doc1/Folder1")
#$sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle}
$dWeb = Get-SPWeb $DestinationWebURL
$dFolder=$dWeb.GetFolder("/MyDoc3/ff/subFoloder")
#$dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle}


$Items = $sFolder.Files

foreach($Item in $Items)
{
    $sBytes = $Item.OpenBinary()
    $dFile = $dFolder.Files.Add($Item.Name, $sBytes, $true)

    $AllFields = $Item.Item.Fields | ? {!($_.sealed)}

    foreach($Field in $AllFields)
    {
        if($Item.Properties[$Field.Title])
        {
            if(!($dFile.Properties[$Field.title]))
            {
                $dFile.AddProperty($Field.Title, $Item.Properties[$Field.Title])
            }
            else
            {
                $dFile.Properties[$Field.Title] = $Item.Properties[$Field.Title]
            }
        }
    }
    $dFile.Update()
}
Write-Host "Script Done"
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top