How to migrate SP library content to destination site?

Ram 20 Reputation points
2023-06-01T12:39:56.8533333+00:00

I know if I would like to migrate the list items in the template then following cmd will work:
Add-PnPDataRowsToSiteTemplate -List “ListName” -Path “PathToTemplateFile” -IncludeSecurity

But I want to do the same for SP library. I have folder structure and files in it. I want to migrate it to destination site.

How may I do that? Please help.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,250 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,309 questions
{count} votes

Accepted answer
  1. Emily Du-MSFT 44,071 Reputation points Microsoft Vendor
    2023-06-02T05:53:23.0666667+00:00

    Based on your description, I understand that you want to migrate an entire document library from one site collection into another site collection by using PowerShell.

    Please run following codes as administrator.

    Note: You need to create a document library in the destination site to storage migrated data first.

    #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://tenant.sharepoint.com/sites/emilytest1"  
    $TargetSiteURL="https://tenant.sharepoint.com/sites/emilytest2"  
       
    $SourceLibraryName="doc"  
    $TargetLibraryName="doc1"  
       
    #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
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

0 additional answers

Sort by: Most helpful