Site Provisioning Using PnP PowerShell


In this article, we will learn how to fetch the site template from the existing site in XML format and apply the same template to another SharePoint site using PnP PowerShell. 

What is Site Provisioning

Site Provisioning is the process of creating SharePoint sites programmatically according to our requirements. We can define our site architecture, or we can also make a template of our already created SharePoint site and apply it to our newly created or already existing SharePoint sites.
Here we will try to achieve site provisioning using PnP PowerShell and will try to apply the template of one SharePoint Communication site to another newly created SharePoint Site.
PnP PowerShell module Should be pre-installed before using the given scripts. Also before running the scripts please redirect to the folder location where we have saved the scripts.

About PnP PowerShell

PnP PowerShell is a cross-platform PowerShell Module providing over 500 cmdlets that work with Microsoft 365 environments and more specifically SharePoint Online, Microsoft Teams, Microsoft Planner, and Microsoft Flow. 

Install PnP PowerShell Module

To install the PnP PowerShell module, we can use the below command in the Windows PowerShell command line
  1. Install-Module -Name PnP.PowerShell  
To install a nightly build of PnP PowerShell:
  1. Install-Module -Name PnP.PowerShell -AllowPrerelease   

Export the Site template

We need to Fetch the site template of the existing site using PnP PowerShell.
  1. $SourceSite =  Read-Host "Enter Source Site URL"  
  2. Connect-PnPOnline -Url $SourceSite -UseWebLogin  
  3. Get-PnPProvisioningTemplate -out SourceSiteTemplate.xml -Handlers All -IncludeAllClientSidePages    
We need to save the script to the appropriate folder and run the script. It will ask for the site URL and Credentials. After that, it will export the SourceSiteTemplate.xml file to the same folder location which contains all site information in XM format.
Given script will export Navigation, Site Pages, and list and library structures without any data.
If we also want to export the list data in XML we need to run the below script for every list.
  1. $listName = Read-Host "Enter the list name"  
  2. Add-PnPDataRowsToProvisioningTemplate -Path ./ SourceSiteTemplate.xml -List $listName -Query '<view></view>'    
We need to make sure that we are connected to the source site using that Connect-PnPOnline command.

Apply the Site template

To Apply the site template that we previously fetched as an XML file on the target site we need to apply the below script.
Before applying the template, we need to make sure that custom scripting is enabled on the targeting site. If custom scripting is already enabled than we skip the step to Enable the custom scripting in our PowerShell script. 
  1. $TagetSite = Read-Host "Enter Target Site URL"  
  2. $Cred= Get-Credential  
  3. $context = New-Object Microsoft.SharePoint.Client.ClientContext($TagetSite)  
  4. $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
  5. $site = $context.Site  
  6. $context.Load($site)  
  7. $context.ExecuteQuery()  
  8. $site.PrimaryUri  
  9. $adminSite = $TagetSite.split('.')[0]+""  
  10. Connect-SPOService -Url $adminSite -Credential $Cred  
  11. Set-SPOsite $site.PrimaryUri -DenyAddAndCustomizePages 0  
  12. $CurrentDirPath = Get-Location  
  13. $templatefilepath="$CurrentDirPath\SourceSiteTemplate.xml"  
  14. Connect-PnPOnline -Url $TagetSite -Credentials $Cred  
  15. Apply-PnPProvisioningTemplate -Path $templatefilepath   
We need to save the script to appropriate folder and run the script. It will ask for the target site URL and Credentials. After that it will start applying xml template to target site.
After completion of the running script, as a result, we will see lists and libraries, Pages and Navigation created from the template.
After completing the Session, we can use the Disconnect-PnPOnline command in the command line to disconnect the session.