0

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.

1
  • Can you share your code to verify what is wrong you are doing
    – Sunil Sahu
    Sep 4, 2018 at 12:47

1 Answer 1

1

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"
1
  • Great ! I converted the code to c# and it works perfect ! thanks a lot
    – Bat
    Sep 6, 2018 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.