SharePoint Online: Copy a Document Library to another Site using PowerShell

Requirement: Copy document library to another site SharePoint Online using PowerShell

sharepoint online copy document library to another site collection

How to Copy a Document Library to another site in SharePoint Online?
There are no direct ways to copy a document library in SharePoint Online. However, You can use this work-around:
  1. Save the source document library as a template - Go to the source library >> Click on Settings gear >> Choose Library Settings >> Click on "Save as Template". More info: How to Save List as Template in SharePoint Online?
  2. Download the List Template and Upload to the Destination Site
  3. Create a new document library from the list template - Login to your destination SharePoint Online site >> Click on Settings gear icon and click "Add an app". You can use the pagination in the bottom of the page to find your custom list template, or use "Find an App" search box and pick your list template. More info: SharePoint Online: Create List from Custom Template using PowerShell
  4. Copy all items from source document library to the new document library - Once you created a new instance of the document library from existing list template, you can copy all files and folders from the source library to the destination library by selecting all items in the source library and choose "Copy to" button in the toolbar and then specify the document library created in step 3.

SharePoint Online: Copy document library to another site collection using PowerShell
To copy a document library with its files and folders, use this PowerShell script.
#Function to Copy library to another site
Function Copy-PnPLibrary
{
    param (
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$SourceSiteURL,
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$DestinationSiteURL,
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$SourceLibraryName,
    [parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$DestinationLibraryName
    )

    Try {
    #Connect to the Source Site
    $SourceConn = Connect-PnPOnline -URL $SourceSiteURL -UseWebLogin -ReturnConnection
    $SourceCtx = $SourceConn.Context

    #Get the Source library
    $SourceLibrary =  Get-PnPList -Identity $SourceLibraryName -Includes RootFolder -Connection $SourceConn

    #Get the List Template
    $SourceRootWeb = $SourceCtx.Site.RootWeb
    $SourceListTemplates = $SourceCtx.Site.GetCustomListTemplates($SourceRootWeb)
    $SourceCtx.Load($SourceRootWeb)
    $SourceCtx.Load($SourceListTemplates)
    $SourceCtx.ExecuteQuery()
    $SourceListTemplate = $SourceListTemplates | Where {$_.Name -eq $SourceLibrary.id.Guid}
    $SourceListTemplateURL = $SourceRootWeb.ServerRelativeUrl+"/_catalogs/lt/"+$SourceLibrary.id.Guid+".stp"

    #Remove the List template if exists    
    If($SourceListTemplate)
    {
        Remove-PnPFile -ServerRelativeUrl $SourceListTemplateURL -Recycle -Force -Connection $SourceConn
    }
    Write-host "Creating List Template from Source Library..." -f Yellow -NoNewline
    $SourceLibrary.SaveAsTemplate($SourceLibrary.id.Guid, $SourceLibrary.id.Guid, "", $False)
    $SourceCtx.ExecuteQuery()
    Write-host "Done!" -f Green

    #Reload List Templates to Get Newly created List Template
    $SourceListTemplates = $SourceCtx.Site.GetCustomListTemplates($SourceRootWeb)
    $SourceCtx.Load($SourceListTemplates)
    $SourceCtx.ExecuteQuery()
    $SourceListTemplate = $SourceListTemplates | Where {$_.Name -eq $SourceLibrary.id.Guid}
    

    #Connect to the Destination Site
    $DestinationConn = Connect-PnPOnline -URL $DestinationSiteURL -UseWebLogin -ReturnConnection
    $DestinationCtx = $DestinationConn.Context
    $DestinationRootWeb = $DestinationCtx.Site.RootWeb
    $DestinationListTemplates = $DestinationCtx.Site.GetCustomListTemplates($DestinationRootWeb)
    $DestinationCtx.Load($DestinationRootWeb)
    $DestinationCtx.Load($DestinationListTemplates)
    $DestinationCtx.ExecuteQuery()    
    $DestinationListTemplate = $DestinationListTemplates | Where {$_.Name -eq $SourceLibrary.id.Guid}
    $DestinationListTemplateURL = $DestinationRootWeb.ServerRelativeUrl+"/_catalogs/lt/"+$SourceLibrary.id.Guid+".stp"

    #Remove the List template if exists    
    If($DestinationListTemplate)
    {
        Remove-PnPFile -ServerRelativeUrl $DestinationListTemplateURL -Recycle -Force -Connection $DestinationConn
    }

    #Copy List Template from source to the destination site
    Write-host "Copying List Template from Source to Destination Site..." -f Yellow -NoNewline
    Copy-PnPFile -SourceUrl $SourceListTemplateURL -TargetUrl $DestinationListTemplateURL -SkipSourceFolderName -Force -OverwriteIfAlreadyExists
    Write-host "Done!" -f Green

    #Reload List Templates to Get Newly created List Template
    $DestinationListTemplates = $DestinationCtx.Site.GetCustomListTemplates($DestinationRootWeb)
    $DestinationCtx.Load($DestinationListTemplates)
    $DestinationCtx.ExecuteQuery()
    $DestinationListTemplate = $DestinationListTemplates | Where {$_.Name -eq $SourceLibrary.id.Guid}

    #Create the destination library from the list template
    Write-host "Creating New Library in the Destination Site..." -f Yellow -NoNewline
    If(!(Get-PnPList -Identity $DestinationLibraryName -Connection $DestinationConn))
    {
        #Create the destination library
        $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListCreation.Title = $DestinationLibraryName
        $ListCreation.ListTemplate = $DestinationListTemplate
        $DestinationList = $DestinationCtx.Web.Lists.Add($ListCreation)
        $DestinationCtx.ExecuteQuery()
        Write-host "Library '$DestinationLibraryName' created successfully!" -f Green
    }
    Else
    {
        Write-host "Library '$DestinationLibraryName' already exists!" -f Yellow
    }
    
    #Copy content from Source to destination library
    $SourceLibraryURL = $SourceLibrary.RootFolder.ServerRelativeUrl
    $DestinationLibrary = Get-PnPList $DestinationLibraryName -Includes RootFolder -Connection $DestinationConn
    $DestinationLibraryURL = $DestinationLibrary.RootFolder.ServerRelativeUrl
    Write-host "Copying Content from $SourceLibraryURL to $DestinationLibraryURL..." -f Yellow -NoNewline

    #Copy All Content from Source Library to the Destination Library
    Copy-PnPFile -SourceUrl $SourceLibraryURL -TargetUrl $DestinationLibraryURL -SkipSourceFolderName -Force -OverwriteIfAlreadyExists
    Write-host "`tContent Copied Successfully!" -f Green

    #Cleanup List Templates in source and destination sites
    Remove-PnPFile -ServerRelativeUrl $SourceListTemplateURL -Recycle -Force -Connection $SourceConn
    Remove-PnPFile -ServerRelativeUrl $DestinationListTemplateURL -Recycle -Force -Connection $DestinationConn
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/Legal"
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/Compliance"
$SourceLibraryName = "Documents"
$DestinationLibraryName = "Documents"

#Call the function to copy document library to another site
Copy-PnPLibrary -SourceSiteURL $SourceSiteURL -DestinationSiteURL $DestinationSiteURL -SourceLibraryName $SourceLibraryName -DestinationLibraryName $DestinationLibraryName

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.