Blog

PnP-PowerShell – How you can get the site template includes pages and web parts and set them to other places (Part 1)

It’s been a while but I finally have enough time to write a post. I’ve got an exciting opportunity to create a provisioning template for using the deployment to other environments using a PowerShell script. I usually don’t like to work with PowerShell Scripts, but thanks to Visual Studio Code (https://code.visualstudio.com) and PnP Framework (https://github.com/SharePoint/PnP-PowerShell) this project was a pleasure. However, creating a template from SharePoint is, unfortunately, a challenge and takes a fair amount of painstaking work. At the moment I only aware ofthree cases.

  1. Content Types
    Don’t add content types to the lists in a sub site, which will be used as the template. After getting the template, if you change or adapt the content types the template is obsolete. In other words if you create new subsite with this ‚old‘ template you’ll get an error message that you have to recreate a new template again.
  2.  Document Sets
    Don’t use document sets in the library. It won’t display as document sets after provisioning. It will display as folders.
  3. Permissions
    Don’t set permission to the site and list/library. Use the script for managing the permission.

For both cases you can use the PowerShell scripts to add content types and document sets after deployment. Now I’ll show you my code for getting a site template with pages and web parts.

Getting site template with pages, web parts and some files

First I create a sub site and adds some lists and libraries. I also created some pages with the web parts and JS-files. The pages are saved in the library ‚Site Pages‘, and the JS-files are saved in a folder ‚Scripts‘ at the library ‚Site Assets‘.

Next, I created a PowerShell script. Here you can see an overview of functions:

 

1. Get a provisioning template with some configuration

This is a default PnP-PowerShell command Get-PnPProvisioningTemplate. This is challenging to make a provisioning template with some configuration. You’ll have to take your time with this configuration and get some experience until you are satisfied with this solution.

2. Save selected page

A function Get-Page($fileName) will save the selected page to locale and add information to provisioning template:

3. Save all web parts

Now the next function Get-WebParts($fileName) will save all web parts as XML-file to locale.

But this information won’t be added to provisioning template. You’ll have to create a PowerShell-script for this.

4. Save all script files

This function Get-ScriptFiles will save all files to locale and add information to provisioning template.

5. Clear the permission

The next function Clear-PermissionsInProvisioningTemplate uses default PnP-PowerShell command Load-PnPProvisioningTemplate. This code will clear all permission in the provisioning template.

Result

So now you have a Provisioning template file with all the files. Thanks to PnP-PowerShell we save a lot of maintenance work and time writing code. You can download the script from github Vintaurus/PnP_GetAndSetSiteTemplate . Hopefully this script works, I didn’t test it myself.

The next post will show how to set the provisioning template with all their files to a new subsite.