Introduction
In the world of SharePoint Online, managing page layouts is crucial for maintaining consistency and enhancing user experience. However, setting the default page layout in Modern SharePoint Online differs from the classic approach, requiring a nuanced understanding of the available tools and methods. In this blog post, we’ll explore how to set default page layout in Modern SharePoint using PnP PowerShell, specifically focusing on the challenges and solutions in Modern SharePoint environments.
Understanding the Challenge
Traditionally, setting the default page layout in SharePoint Online was straightforward, thanks to the Set-PnPDefaultPageLayout cmdlet in PnP PowerShell. However, as of the current scenario, this approach is tailored for publishing sites or classic sites, posing a dilemma for Modern SharePoint Online users.
It’s worth noting that while PnP PowerShell commands continue to evolve and adapt to the changing landscape of SharePoint Online, support for Modern SharePoint environments may vary with updates and releases. Therefore, it’s essential to stay informed about the latest developments and enhancements in PnP PowerShell to leverage its full potential in Modern SharePoint Online scenarios.
Prerequisites
Before getting started with setting up the default page layout in Modern SharePoint Online using PnP PowerShell, ensure you have the following prerequisites in place:
- PnP PowerShell Module: Install the PnP PowerShell module on your local machine. If you need guidance on installing the module, refer to my earlier blog post: PnP PowerShell: How to get started? – Hemant Kabra
Solution
In Modern SharePoint Online, setting the default page layout involves leveraging PnP PowerShell alongside other commands to manipulate site properties effectively. Below, we outline a step-by-step guide along with sample code to illustrate this process:
Step 1: Get the Page File
First, retrieve the desired page file using the Get-PnPPage cmdlet, specifying the identity of the custom page template.
$page = Get-PnPPage -Identity "Templates/CustomPageTemplate.aspx"
$pageFile = $page.GetPageFile()
Step 2: Update Site Property
Next, ensure that scripting capabilities are enabled on the site by temporarily setting NoScriptSite to false. This step is essential for modifying property bags.
Set-PnPSite -Identity https://mytenant.sharepoint.com/sites/mysite -NoScriptSite $false
IMPORTANT NOTE
The “NoScriptSite” property in SharePoint Online controls whether custom script is allowed to run on a site. When set to “true,” it prevents users from running custom script in the site collection. This property is often managed as part of the SharePoint Online administration settings.
By default, when a new Microsoft 365 tenant is created, the “NoScriptSite” property is typically set to “true.” This means that custom script capabilities are disabled at the site collection level for security reasons. This default setting helps prevent potential security vulnerabilities associated with running custom scripts.
For more details about this, please refer to the official documentation from Microsoft available at: Allow or prevent custom script – SharePoint in Microsoft 365 | Microsoft Learn
Step 3: Set Default Page Layout
Add the unique identifier of the custom page template to the site’s property bag using the Set-PnPPropertyBagValue cmdlet.
Set-PnPPropertyBagValue -Key "DefaultNewPageTemplateId" -Value $pageFile.UniqueId
Tip: You can also use Add-PnPPropertyBagValue instead of Set-PnPPropertyBagValue as this is an alias for Set-PnPPropertyBagValue
Step 4: Finalize Configuration
Once the default page layout is set, revert the scripting capabilities to their original state by setting NoScriptSite to true.
Set-PnPSite -Identity https://mytenant.sharepoint.com/sites/mysite -NoScriptSite $true
Conclusion
In conclusion, while the Set-PnPDefaultPageLayout cmdlet may not currently support Modern SharePoint Online, the landscape of SharePoint development is dynamic, with constant updates and improvements. As PnP PowerShell continues to evolve, there’s a possibility that future updates may extend support to Modern SharePoint environments, simplifying tasks such as Set default page layout in Modern SharePoint using PnP PowerShell. Therefore, staying engaged with the SharePoint community and keeping abreast of the latest developments in PnP PowerShell is crucial for maximizing productivity and efficiency in SharePoint Online environments.