2

I'm using SharePoint 2013

I need to copy files/folders/sub folders from one site collection to different site collection(basically archiving them).

I need to do this remotely by using CSOM/JSOM. Can we maintain metadata after copied files / folders? If so, please share some code.

1

Powershell Script to dump all the folders from one list to another in same or different site collection with metadata . Try it on sample list and test first .

function Copy-ItemsToFolder([Microsoft.SharePoint.SPFolder]$folder, $srcList, $destList)
{
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.Folder = $folder


$sourceSPListItemCollection = $srcList.GetItems($spQuery);
$destFolder = $destList.Folders[$folder.Name];
if ($destFolder -eq $null -or $destFolder -eq "")
{
    #folder does not exist so create it.
    $destFolder = $destList.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
    $destFolder["Title"] = $folder.Name ;
    $destFolder.Update()
}
$count = $sourceSPListItemCollection.Count;
$index = 0;
$newFolderPath = $destFolder.Folder.ServerRelativeUrl
foreach($srcListItem in $sourceSPListItemCollection)
{
    $index++;

    write-host "adding new item $index of $count";
    $newSPListItem = $destList.AddItem($newFolderPath, [Microsoft.SharePoint.SPFileSystemObjectType]::File, $null);

    foreach($spField in $srcListItem.Fields)
    {
        if ($spField.ReadOnlyField -ne $True -and $spField.InternalName -ne "Attachments")
        {
            $newSPListItem[$spField.InternalName] = $srcListItem[$spField.InternalName];
        }
    }
    $newSPListItem.Update()
    write-host "added";
}
}
try
{
$srcListSiteUrl = "https://sitename/abc/"
$SourceListName = "lsitname"

$dstListSiteUrl = "https://sitename/def/"
$DestinationListName = "lsitnamearchieve"

$keyColumnInternalName = "Title"

$sourceListWeb = Get-SPWeb -identity $srcListSiteUrl
$sourceListUrl = $sourceListWeb.ServerRelativeUrl + "/lists/" + $SourceListName;

$dstListWeb = Get-SPWeb -identity $dstListSiteUrl
$destinationListUrl = $dstListWeb.ServerRelativeUrl + "/lists/" + $DestinationListName;

$SourceList = $sourceListWeb.GetList($sourceListUrl);
$DestinationList = $dstListWeb.GetList($destinationListUrl);

foreach($currentFolder in $SourceList.RootFolder.SubFolders)
{
    Write-Host "processing folder $($currentFolder.Name)";

    if ($currentFolder.ItemCount -gt 0)
    {
        Copy-ItemsToFolder $currentFolder $SourceList $DestinationList
    }
    Write-Host "done with folder"
}

}

catch
{
write-host $_.exception
}

finally
{
if($sourceListWeb -ne $null){$sourceListWeb.Dispose()}
if($dstListWeb -ne $null){$dstListWeb.Dispose()}
}
  • Thx for taking time to respond. Let me implement and let you know how it worked for me. – SP007 Aug 15 '14 at 14:33
  • Just a quick reminder: you need to have all the fields from the source list in the destination list. – Jaime Aug 14 '15 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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