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:


Connect-PnPOnline -Url "https://<your-site-url>&quot;
# Assumes that you already have a page named IT.aspx
# created and ready to be used as a template
$page = Get-PnPClientSidePage -Identity "IT.aspx"
# Save the page as template to be reused later-on
$page.SaveAsTemplate("IT_Template.aspx")

Using PnP Core C#:


using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using ClientSidePage = OfficeDevPnP.Core.Pages.ClientSidePage;
namespace ConsoleApp1
{
class p3
{
static void Main(string[] args)
{
string userName = "username";
string password = "password";
using (ClientContext clientContext = new ClientContext("https://<your-site-url>&quot;))
{
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
// Load the page
ClientSidePage page = clientContext.Web.LoadClientSidePage("IT.aspx");
// Save the page as template for later use
page.SaveAsTemplate("IT_Template.aspx");
}
}
}
}

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:


Connect-PnPOnline -Url "https://<your-site-url>&quot;
# Get the template from the template folder
$page = Get-PnPClientSidePage -Identity "Templates/IT_Template"
# Save the page with a new name based on the template
$page.Save("IT-India.aspx")

Using PnP Core C#:


using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using ClientSidePage = OfficeDevPnP.Core.Pages.ClientSidePage;
namespace ConsoleApp1
{
class p3
{
static void Main(string[] args)
{
string userName = "username";
string password = "password";
using (ClientContext clientContext = new ClientContext("https://<your-site-url>&quot;))
{
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
// Load the template page
ClientSidePage page = clientContext.Web.LoadClientSidePage("Template/IT_Template.aspx");
// Create and Save the modern page based on the template
page.Save("IT_Australia.aspx");
}
}
}
}

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 ?

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

    1. Hey Corrie, thanks a lot ! All is ok here, hope it is the same there !
      Just a correction, was supposed to be in Finland not UK , but then “the situation” happened. So currently still in India, but once the situation calms down, will be flying there.

      Really glad you found this post helpful !!
      Regards.

      Like

Leave a comment

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