In the previous blogs here, we have looked at the Provisioning process for a complex Team site. Much of complexity was easily handled by the PnP Provisioning process.

In this blog, we will look at the similar Provisioning process but from an Admin point of view and use PnPPowerShell for create and provision the site.

Steps:
The steps are actually quite simple and could be done quickly.

  1. Build a Template Site to be used for creating the Provisioning Template
  2. Manually apply changes to the Template site and extract the Provisioning template from the Template Site using the Get-PnPProvisioningTemplate
  3. Create the SharePoint Team site to apply the template
  4. Apply the Provisioning template to the above site using the Template obtained in Step 2
  5. Finalize the creation with any remaining changes.

Before we start looking at the above steps in detail, below are few key items to keep in mind during the provisioning process. Some of the these will become clear as we go through the detailed process steps

1. During the Get Template process, one of the key switches is to include/exclude any dependancy items which are not related to the new site. More details with Get-PnPProvisioningTemplate section

2. During the apply template process, only the differential changes are applied. Hence it is safe to apply the template many times or repeatedly in case of an error occurs during the process

3. Since the apply template process only adds differential items, any explicit changes for eg. left navigation default links removal needs to be done after the apply template process is complete.

4. The apply template works by matching the Provisioning Template schema with latest PnP code schema. Hence, if you are working with a old template then use the Schema switch as specified below.

Get Template Process

The first step would be to install the PnP PowerShell module. Check this blog here to see how to get started with PnP PowerShell.

Next create a template site that will be used to create the new sites.

After the site is created, we could create a template from it using the Get-PnPProvisioningTemplate cmdlet. Some of the key switches while creating the template are below.


Get-PnPProvisioningTemplate -Out "<Folder location in drive>"
-ExcludeHandlers ApplicationLifecycleManagement, SiteSecurity
-ExcludeContentTypesFromSyndication

Helpful Switches for Get-PnPProvisioningTemplate cmdlet

-ExcludeHandlers – Custom handlers to exclude settings or elements from the site sucha as ApplicationLifecycleManagement, Site Security, WebApiPermissions etc.
-ExcludeContentTypesFromSyndication – Exclude the Content Types pushed from the Hub. This is generally helpful if content types no longer persist and could cause conflict issues.
-ExtensibilityHandlers – Allows to specify ExtensbilityHandlers to execute while extracting a template for doing custom actions such as exporting pages schema into PnP export
-Handlers – Custom handlers to explicitly include settings or elements from the site such as Audit settings, Features, Fields, Search Settings, Term Groups etc.
-Schema – PnP Schema version of the exported template
-IncludeSearchConfiguration – Include the Search Configuration of the site
-PersistMultiLanguageResources – Persist Multilingual files

Apply Provisioning Template Process

After the template is extracted, we could apply this template to any newly created site. Before application of the template, we will create a new modern site using the PnP PowerShell command below


## Create a SharePoint classic site
New-PnPTenantSite -Title "Title" -Url "https://tenant.sharepoint.com/sites/site1&quot; -Lcid 1033 -TimeZone <TimeZoneID> -Template "STS#0" -StorageQuota <Size in MB> -StorageQuotaWarningLevel <Size in MB> -ResourceQuota <quota number> -Description "description" -Owner "owner@tenant.com" -RemoveDeletedSite -Wait
## Create a new Modern SharePoint site
New-PnPSite -Title "Title" -Url "https://tenant.sharepoint.com/sites/test&quot; -Type CommunicationSite -SiteDesign Showcase -Description "description"

Next is to apply the template which takes about 20-25 minutes for a complex template with about 20 libraries, 30 site columns, 15 content types etc. Since the process takes so much time, it would be good to trace log any issues and error handle the application process.

The Apply Provisioning Template cmdlet details are below. It is important to note some of the switches for the cmdlet as it does apply some custom settings for the template.


Set-PnPTraceLog -On -LogFile "<Location in hard drive>" -Level Debug
Apply-PnPProvisioningTemplate -Path "<Folder location>" -ClearNavigation -IgnoreDuplicateDataRowErrors

Helpful Switches for Apply-PnPProvisioningTemplate cmdlet

-ClearNavigation – Clear the Navigation of the site before template is applied
-IgnoreDuplicateDataRowErrors – Ignores to stop the script because of duplicate daa errors
-ExcludeHandlers – Exclude the handlers when applying template
-ExtensibilityHandlers – Apply the extensibility handlers for the custom scripts

Conclusion

In this blog we saw how we could use PnP PowerShell and PnP Provisioning Template to apply custom template to a newly created site in order to automate the creation process.

8 Comments

  1. Hello,
    I am getting a “Value does not fall within the expected range” error when I try to create SiteCollection using New-PnPTenantSite

    Exception : Microsoft.SharePoint.Client.ServerException: Value does not fall within the expected range.
    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
    at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext clientContext, Int32 retryCount, Int32 delay, String userAgent)
    at Microsoft.SharePoint.Client.TenantExtensions.CreateSiteCollection(Tenant tenant, SiteEntity properties)
    at SharePointPnP.PowerShell.Commands.NewTenantSite.ExecuteCmdlet()
    at SharePointPnP.PowerShell.Commands.PnPCmdlet.ProcessRecord()

    Like

    1. Hi Pankaj, there could be few reasons, one could be that New-PnPTenantSite is for classic SharePoint sites, so if you if classic site collections are turned off, which in most cases happens could cause it. Also see if you have SharePoint Admin Rights for the tenancy.

      Like

  2. Hello Ashish, I am working on SP 2016 OnPremise and trying to create site collection with template STS#1. I am using the SharePoint admin account for site creation. Upon further investigation I found the below error in logs:
    System.ArgumentException: Value does not fall within the expected range. at Microsoft.SharePoint.Library.SPRequestInternalClass.SscCreateSite(Guid gApplicationId, String bstrUrl, String bstrServerRelativeUrl, Int32 lZone, Guid gSiteId, Guid gDatabaseId, String bstrDatabaseServer, String bstrDatabaseName, String bstrDatabaseUsername, String bstrDatabasePassword, String bstrTitle, String bstrDescription, UInt32 nLCID, String bstrWebTemplate, String bstrOwnerLogin, String bstrOwnerName, String bstrOwnerEmail, String bstrSecondaryContactLogin, String bstrSecondaryContactName, String bstrSecondaryContactEmail, Boolean bADAccountMode) at Microsoft.SharePoint.Library.SPRequest.SscCreateSite(Guid gApplicat… e3066f3f-0715-472d-b67e-e59fee093a90

    There are seveal posts that suggest adding the App pool account to farm administrators group. I have tried this also but I still have the issue.

    Like

    1. Hi Pankaj, are you using CSOM or Server Object Model ? From the error it seems like it is failing while creating a library. I would recommend to open the PnP or XML file and look for the GUID corresponding to the library, and then see if anything is wrong with that library

      Like

  3. This is awesome and exactly what I have been looking for. The only piece I need is how to automate this. Currently I have a Site Creation SharePoint site that uses flow to create the Azure job to create the site, but I need to know how to add the Apply Template piece to that process? Any guidance would be appreciated.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s