How to Copy a Site using PowerShell PnP Provioning Template in SharePoint Online?

Requirement: Copy a sub-site in SharePoint Online using PnP Provisioning Template.

Site templates are a great way to make a "template" and create a copy of a subsite including all its structure and optionally content. However, Microsoft has removed the "Save as Template" feature in modern site templates and group associated team site or communication sites. So, How to make a copy of a site in SharePoint Online?
copy sharepoint online site using pnp provioning template powershell

How this process works?
PnP PowerShell is a powerful way to provision site artifacts in SharePoint Online. Now, let's use it to copy a subsite using PnP provisioning template.
  • Step 1: Connect to the Source site and Get its Schema
  • Step 2: Create a New Subsite
  • Step 3: Import the Schema to the Target Site

Step 1: Get the Source Site Schema XML
Create your site using the SharePoint UI, do the necessary customizations. Once the site is ready to use as a template, Connect to the source site and get the site schema in XML format using Get-PnP ProvisioningTemplate cmdlet.
#Set variables
$SiteURL = "https://crescent.sharepoint.com/sites/projects/london"
$SchmaXMLPath = "C:\Temp\SiteSchema.xml"

#Connect to PnP Online
Connect-PnPOnline -Url $siteUrl -UseWebLogin 

#Get Site Schema
Get-PnPProvisioningTemplate -Out ($SchmaXMLPath) -PersistBrandingFiles -PersistPublishingFiles
This cmdlet gets all artifacts from the source site, such as content types, site columns, term store, list and libraries, theme, pages, etc. into the given template XML file. We also have a switch -includesitegroups to include site security, so that your target site will have unique permissions.

Step 2: Create a New Subsite
Create a new subsite of the same template as the source site.
#Set variables
$SiteURL = "https://crescent.sharepoint.com/sites/projects"

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

#Create a new subsite
$NewWeb = New-PnpWeb -Title "Project Morocco" -Url "morocco" -Description "Team Site for Morocco Project" -Locale 1033 -Template "STS#3"
The next step is to get the target site and apply the template to it. The exported XML file can be applied to any other site to turn them into a copy of the original site.

Step 3: Apply the Site Schema into Target Site with Apply-PnP ProvisioningTemplate
Once the subsite is created, apply the to the source site and get the site schema in XML format.
#Set variables
$SiteURL = "https://crescent.sharepoint.com/sites/projects/morocco"
$SchmaXMLPath = "C:\Temp\SiteSchema.xml"

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

#Apply Pnp Provisioning Template
Apply-PnPProvisioningTemplate -Path $SchmaXMLPath -ClearNavigation

Please note, the PnP Provisioning template method only extracts the site template without its contents. If you need to copy the site with its content, You need to do it either with PowerShell scripts or with any migration tool. Also, you can't extract the site template of a root site and apply it on subsites! Here is the PnP provisioning documentation: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/pnp-provisioning-framework

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.