Copy Document Library from one site collection to another in SharePoint Online

Sajith Gopalakrishnan Hema 1,056 Reputation points
2021-01-11T10:46:21.877+00:00

How to copy document library from one site collection to another in SharePoint online by preserving the permission, modified date etc.

Is there any option without using tool ?

Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

1 answer

Sort by: Most helpful
  1. JoyZ 18,111 Reputation points
    2021-01-12T09:02:44.97+00:00

    Hi @Sajith Gopalakrishnan Hema ,

    We suggest you use powershell to copy files in document library to another site along with Metadata values, which means that "Modifiled" value will not change:

    #Load SharePoint CSOM Assemblies  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
       
    Function Copy-AllFilesWithMetadata  
    {  
      param  
        (  
            [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,  
            [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder  
        )  
        Try {  
            #Get all Files from the source folder  
            $SourceFilesColl = $SourceFolder.Files  
            $SourceFolder.Context.Load($SourceFilesColl)  
            $SourceFolder.Context.ExecuteQuery()  
       
            #Iterate through each file and copy  
            Foreach($SourceFile in $SourceFilesColl)  
            {  
                #Get the source file  
                $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceFolder.Context, $SourceFile.ServerRelativeUrl)  
                   
                #Copy File to the Target location  
                $TargetFileURL = $TargetFolder.ServerRelativeUrl+"/"+$SourceFile.Name  
                [Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetFolder.Context, $TargetFileURL, $FileInfo.Stream,$True)  
       
                #Copy Metadata field values  
                $SourceListItem = $SourceFile.ListItemAllFields  
                $SourceFolder.Context.Load($SourceListItem)  
                $SourceFolder.Context.ExecuteQuery()  
                   
                #Get the new file created  
                $TargetFile = $TargetFolder.Context.Web.GetFileByServerRelativeUrl($TargetFileURL)  
                $TargetListItem = $TargetFile.ListItemAllFields  
                   
                #Set Metadata values from the source  
                $Author =$TargetFolder.Context.web.EnsureUser($SourceListItem["Author"].Email)  
                $TargetListItem["Author"] = $Author  
                $Editor =$TargetFolder.Context.web.EnsureUser($SourceListItem["Editor"].Email)  
                $TargetListItem["Editor"] = $Editor  
                $TargetListItem["Created"] = $SourceListItem["Created"]  
                $TargetListItem["Modified"] = $SourceListItem["Modified"]  
                $TargetListItem.Update()  
                $TargetFolder.Context.ExecuteQuery()  
       
                Write-host -f Green "Copied File '$($SourceFile.ServerRelativeUrl)' to '$TargetFileURL'"  
            }  
       
            #Process Sub Folders  
            $SubFolders = $SourceFolder.Folders  
            $SourceFolder.Context.Load($SubFolders)  
            $SourceFolder.Context.ExecuteQuery()  
            Foreach($SubFolder in $SubFolders)  
            {  
                If($SubFolder.Name -ne "Forms")  
                {  
                    #Prepare Target Folder  
                    $TargetFolderURL = $SubFolder.ServerRelativeUrl -replace $SourceLibrary.RootFolder.ServerRelativeUrl, $TargetLibrary.RootFolder.ServerRelativeUrl  
                    Try {  
                            $Folder=$TargetFolder.Context.web.GetFolderByServerRelativeUrl($TargetFolderURL)  
                            $TargetFolder.Context.load($Folder)  
                            $TargetFolder.Context.ExecuteQuery()  
                        }  
                    catch {  
                            #Create Folder  
                            if(!$Folder.Exists)  
                            {  
                                $TargetFolderURL  
                                $Folder=$TargetFolder.Context.web.Folders.Add($TargetFolderURL)  
                                $TargetFolder.Context.Load($Folder)  
                                $TargetFolder.Context.ExecuteQuery()  
                                Write-host "Folder Added:"$SubFolder.Name -f Yellow  
                            }  
                        }  
                    #Call the function recursively  
                    Copy-AllFilesWithMetadata -SourceFolder $SubFolder -TargetFolder $Folder  
                }  
            }  
        }  
        Catch {  
            write-host -f Red "Error Copying File!" $_.Exception.Message  
        }  
    }  
       
    #Set Parameter values  
    $SourceSiteURL="https://crescent.sharepoint.com/sites/sales"  
    $TargetSiteURL="https://crescent.sharepoint.com/sites/Ops"  
       
    $SourceLibraryName="Project Documents"  
    $TargetLibraryName="Documents"  
       
    #Setup Credentials to connect  
    $Cred= Get-Credential  
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
       
    #Setup the contexts  
    $SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)  
    $SourceCtx.Credentials = $Credentials  
    $TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)  
    $TargetCtx.Credentials = $Credentials  
            
    #Get the source library and Target Libraries  
    $SourceLibrary = $SourceCtx.Web.Lists.GetByTitle($SourceLibraryName)  
    $SourceCtx.Load($SourceLibrary)  
    $SourceCtx.Load($SourceLibrary.RootFolder)  
       
    $TargetLibrary = $TargetCtx.Web.Lists.GetByTitle($TargetLibraryName)  
    $TargetCtx.Load($TargetLibrary)  
    $TargetCtx.Load($TargetLibrary.RootFolder)  
    $TargetCtx.ExecuteQuery()  
       
    #Call the function  
    Copy-AllFilesWithMetadata -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder  
    

    Please remember to replace parameter values for yourself, in addition, in above scripts, the folder will be newly created rather than copy, so the "modified" value of folder will change.

    Since this code will inherit the permissions of the target site, it is recommended that you re-grant the permissions of the target library, which means that we cannot retain permissions when copying files.

    Mover is used to migrate data from other cloud service providers, such as moving from Office 365 to a new Office 365 domain, not for site collection migration.


    If an Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.