PnP Automatic Site Creation Based on Template

Creating a SharePoint subsite based on an existing template using the new SharePoint PnP tools

SharePoint Online Logo Microsoft Partner

We covered the launch of the long-awaited PnP automation tools in a previous post. This post covers SharePoint PnP Automatic Site Creation using these very powerful tools.

While working on an Azure & SharePoint migration for one of our clients a requirement to mass create a few hundred sub sites based on a template.

Part of our SharePoint deployment toolkit includes a standardised project and client management area in which each project or client has an individual sub site tailored to it based on the customers’ requirements.

SharePoint PnP Automatic Site Creation

In this case, we’d deployed a client management area to support their existing Dynamics deployment and wanted to create all of their existing customers based on this template, the out of the box Dynamics / SharePoint integration wasn’t enough for their requirements in this case. Historically we could save the site as a template and manually create the subsites based on this (a very long and arduous process). PnP tools for SharePoint simplifies the whole process and allows us to create a powershell script that achieves the follow,

  1. Look at a site and download all of this information to a template file.
  2. Create a new SharePoint Sub Site
  3. Apply the template to the SharePoint Site
  4. Run through an Excel file and create hundreds or thousands of subsites very easily.

To get started with PnP tools we need to install the prerequisites and tools found here,

Once installed PnP tools allow us to create a sub site template in a similar way to the old method and export this as a PnP Provisioning template using the following commands,

Get-PnPProvisioningTemplate -Out C:Tempcustomertemplate.xml -PersistBrandingFiles

Get-PnPProvisioningTemplate -Out C:Tempcustomertemplate.pnp -PersistBrandingFiles

Once we have a template exported to a file we can deploy a new web using the following command, effectively creating a blank sub site based on the team site template.

New-PnPWeb -Title $siteTitle -Url $siteUrl -Template $siteTemplate -InheritNavigation

Once the site had been fully created we can then use our template to apply the exact same structure to this site (document libraries, lists, branding, navigation, etc.).

Apply-PnPProvisioningTemplate -Path C:Tempcustomertemplate.pnp

For our client, we built a script that reads all of the required customer subsites from an excel spreadsheet (exported from Dynamics). This sets the Site Name to the Account Name, URL is the Site Name stripping any illegal characters and automatically applies our customer template to each one. As customers are always being added to Dynamics this was extended even further in which all new clients are automatically exported on a nightly basis and then created into SharePoint removing many steps of manual site creation and reducing the chance of confusion.

The basic full script can be found below,


#get and save your O365 credentials
$cred = Get-Credential -Message "Enter SPO credentials"

$url = ""
Connect-PnPOnline –Url $url –Credentials $cred
Get-PnPProvisioningTemplate -Out C:Temppnpdemo.xml -PersistBrandingFiles
Get-PnPProvisioningTemplate -Out C:Temppnpdemo.pnp -PersistBrandingFiles

$url = ""
Connect-PnPOnline –Url $url –Credentials $cred

$siteTitle = "My New Site 1"
$siteUrl = "my-new-site-1"
$siteTemplate = "STS#0"

New-PnPWeb -Title $siteTitle -Url $siteUrl -Template $siteTemplate -InheritNavigation
#connect to the new web site using the stored credentials
Connect-PnPOnline –Url $url/$siteUrl –Credentials $cred
Apply-PnPProvisioningTemplate -Path C:Temppnpdemo.pnp

Talk to Us

At Valto, we’re committed to helping our clients get the most out of Office365 & SharePoint. Our mentality is to work smarter not harder and automating time consuming jobs is a huge part of that. Talk to us about how we can help you make better use of SharePoint whether you choose on premise or Online.

Call now on 03335 779 009 or Contact Us.

Other blog posts in this series

Microsoft Sharepoint Icon from Valto Microsoft Partner
SharePoint Online Square Microsoft Partner