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 site 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?
How to copy a Site Collection in SharePoint Online using PowerShell?
PnP PowerShell is a powerful way to provision site artifacts in SharePoint Online, We can use it to duplicate a subsite. 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 customization. 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-PnPSiteTemplate 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 Site Collection or 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 Invoke-PnPSiteTemplate
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