SharePoint Online: Copy List or Document Library to Another Site using PnP PowerShell

Requirement: Copy Document Library to another site using PowerShell

SharePoint Online: Copy Document Library to Another site using PnP PowerShell
PnP provisioning engine lets us to copy site objects such as lists and libraries, site columns, content types, composed looks, pages, etc. We can export existing customization to XML file and use it as a template to create new objects from it. We can copy lists and libraries between SharePoint Online site collections with the help of PnP Provisioning engine. Here is the PowerShell script:

Step 1: Get the List Schema from Source Site
With PnP provisioning engine, we need to extract the list schema first:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$ListName = "Project Tasks"
$TemplateFile = "C:\Temp\ListSchema.xml"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get the List schema as Template and export to a File
$Templates = Get-PnPProvisioningTemplate -OutputInstance -Handlers Lists
$ListTemplate = $Templates.Lists | Where-Object { $_.Title -eq $ListName }
$Templates.Lists.Clear()
$Templates.Lists.Add($ListTemplate)
Save-PnPProvisioningTemplate -InputInstance $Templates -Out $TemplateFile
This script creates a XML template file that contains all list schema from SharePoint Online site, and then filters to get the specific list.

Step 2: Apply the Template in the Target Site
Once we have the template XML file ready, we can import it to any site collection, as:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Operations/"
$TemplateFile = "C:\Temp\ListSchema.xml"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

Write-Host "Creating List from Template..."
Apply-PnPProvisioningTemplate -Path $TemplateFile
This PowerShell script copies list or document library from source site to the destination site collection.
SharePoint Online: Copy List or Document Library to Another Site using PnP PowerShell SharePoint Online: Copy List or Document Library to Another Site using PnP PowerShell Reviewed by Salaudeen Rajack on February 19, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.