People keep asking me how they can create templates of SharePoint sites leveraging the PnP framework (which I talk about a lot lately). They want to "clone" an existing site into new team sites, projects sites or company sites.
There are countless other use cases for creating templates. For many years now I have had this requirement in my projects. There is an old (German) blog post about this topic from 2013. That's 6 years ago!
The good news is: it's quite easy to create a template. Thanks to the PnP community there is a pleasant set of PowerShell commands (called cmdlets, that's "commandlets") we can use to create templates in no time.
Let me show you how this works.
Install the PowerShell Cmdlets
To start creating site collection templates you need some cmdlets. The easiest way to get them is by installing a PowerShell module. In Windows 10 you can do this directly via PowerShell:
Install-Module SharePointPnPPowerShellOnline
(If this doesn't work for you there are also setup files which can be downloaded here. We had to use them sometimes due to proxy issues.)
This installs the cmdlets you use for working with SharePoint Online. Note that some of them will also work on-premises but some will fail.
There are tailored modules available for SharePoint 2013, 2016 and 2019 as well. They are stripped down to the features those versions of SharePoint provide. More information and instructions can be found here: PnP PowerShell overview.
You need administrative rights to install the module since it will be installed globally. If you don't want to grant those rights you can use the following command:
Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser
The Scope parameter makes the module install for the current user only. After installing you are ready to create your first template.
Export a Site Collection as Template
You export a site collection as template with only two commands:
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/template"
# save template.xml to current directory; you could also use an absolute path for the -Out parameter
Get-PnPProvisioningTemplate -Handlers All -Out "template.xml" -IncludeAllClientSidePages -PersistBrandingFiles
Connect-PnPOnline (despite the name this is also used on-premises) creates a connection to the site collection you want to export. Depending on the farm's configuration (e.g. multi-factor authentication) you might have to add more parameters.
Finally, the cmdlet Get-PnPProvisioningTemplate exports the site to a file named template.xml. The -PersistBrandingFiles parameter saves branding files like the site logo in a sub folder. The -IncludeAllClientSidePages parameter exports all modern pages as well.
That's all. The file template.xml now contains a description of your site collection. This is a description of every (non-standard) list, content type, permission, field, menu entry, configuration etc. that make up your site.
The next step is creating a new site collection based on this template.
Create a new template-based Site Collection
It's done in two steps:
- Create a new out-of-the-box standard site collection
- Apply the template to the new site collection
You achieve this with the following commands:
Connect-PnPOnline -Url "https://tenant.sharepoint.com"
New-PnPSite -Type CommunicationSite -Title "Demo Site" -Url "https://tenant.sharepoint.com/sites/new"
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/new"
Apply-PnPProvisioningTemplate -Path "template.xml" -Handlers All -ClearNavigation
Lines 1 and 2 create the new communication site "Demo Site". Whether this is done via PowerShell or other means depends on your use case. The site collection could also be created using a different provisioning mechanism.
Lines 3 and 4 connect to the newly created site collection and apply the provisioning template exported in the last section. The -ClearNavigation parameter prevents creating duplicate menu entries.
Done.
Restrictions when Creating Templates
The PnP Provisioning Framework has some restrictions. Those can be surprising show stoppers if you don't know them beforehand.
The template does not include:
- List elements
- Files from document libraries
- Modern pages, except the home page (WORKS AS OF 07/2019*)
There are separate cmdlets and projects available to circumvent some of those restrictions.
You can use the cmdlet Add-PnPDataRowsToProvisioningTemplate to export list elements to the template file. To export all modern pages you have to use an extensibility handler which is available as source code. I created a ticket on GitHub suggesting to include this functionality in the core framework. Let's see.
*UPDATE 07/2019: There now is a -IncludeAllClientSidePages parameter that adds all modern pages to the template as well. No need to use the extensibility handler anymore. Learn more in this article.
Summing it up
Using the PnP Provisioning Framework it's straightforward to create site collection templates.
Those templates are XML files describing the (non-standard) structure and configuration of a site collection. Such a template is applied to an out-of-the-box site collection resulting in a site that is similar to the template site.
Content like list elements, files and modern pages are (by default) not part of the template.
Happy templating!
Comments