How to : Create modern page templates and provision modern pages using them via PnP PowerShell/Core

Sometime in the first half of 2019, Microsoft began the roll out of the modern page templates. Page templates can be kinda considered as a better and a modern replacement of page layouts which we were used to in the old days of master page and page layout customisation. The modern sites are by default NoScript and don’t allow customisation via Master pages and Page layouts, hence the need for page templates.

Page templates, as the name suggests, allows you to quickly provision pages based on a template and provide a consistent look and feel. They are also a great way for end users who can use them as a starting point to do their own customisation.

While exploring this functionality, I came across 2 blogs by Mikael Svenson and Beau Cameron which mentioned this capability.

Based on these excellent blogs, we now know that these templates are stored in a folder named Templates inside the Site Pages library. What distinguishes this folder is the property bag value. Initially whenever a template is created, SharePoint creates the folder named Template and sets the property bag vti_TemplatesFolderGuid key’s value to that of the GUID of the folder (if you are using PnP PowerShell/Core, you don’t need to worry about the details as it is handled internally, seriously thank the PnP Core team !) .

What I couldn’t find in documentation, was a way to create these templates as well as create pages from these templates programmatically. So, again I decided to dig in found something !!!

Consider the scenario of an IT company having offices across the globe. Now, we want to create a template which can used by these offices. Below, I have mentioned 2 approaches using PnP PowerShell and PnP CSOM core for C# devs:

Create Modern Page templates :

Using PnP PowerShell:

Using PnP Core C#:

Create Modern page from page templates :

Now that we have created a page template, its time to create a modern page from it. Now, as mentioned above, the IT company has offices in India, Netherlands and Australia. We will use the previously created template to create a page for the India office. Again, we can use PnP PowerShell as well as PnP CSOM Core for C# devs:

Using PnP PowerShell:

Using PnP Core C#:

To use the above snippets, you need to use either PnP PowerShell or PnP CSOM Core library.

PS:

1) As Beau mentioned in his blog, page templates can be created by Site Owners or Site collection admins, so do keep that in mind while executing the above code.

2) The major advantage of page templates is that all the pages that are created using the template automatically get the content( including text, videos, images etc) as well as the OOTB and SPFx webparts pre-configured i.e they will take the configuration of the page template.

3) Once a page template is created, any change that you make is not propagated to pages previously created. Only the new pages that you create will have these changes reflected.

4) The beauty of using PnP extensions methods is that it handles everything ! So, if you are creating a template for the first time, it will internally create the Templates folder and also set its property bag value. Pretty darn cool, isn’t it ?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.