Working With Page Layouts On SharePoint Using PnP PowerShell

Introduction

In this article, you will learn how we can create, add, retrieve, and delete the page layouts on SharePoint, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

By default, the page layouts are available on the master page gallery of the site. The URL is http://siteurl /_catalogs/masterpage/Forms/AllItems.aspx.

Prerequisite

You need to have PowerShell 3.0, available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation on the official site. The installers are available here. Online version installer is preferred and is named On Premise or Office 365 operations. You can also install all three installers for testing (SharePoint 2013, 2016, online).

The PnP PowerShell is supported from SharePoint 2013 On Premise and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments.

Connect To Site

Connect to the site using the snippet given below. The PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM):

  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  

Once connected, you can carry out any of the operations mentioned below, based on the requirement. 

Create Page Layout

The page layouts can be created by users on the SharePoint sites. The following operation should be carried out.

Create an application page on local system

Create a new application page with .aspx extension, and paste the below content. In the content add necessary tag prefixes, web part zones and web parts in the respective zones. For example, name it TestLayout.aspx.

  1. <%@ Page language="C#"   Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" %>  
  2. <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>   
  3. <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>   
  4. <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>   
  5. <%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>  
  6.   
  7.   
  8. <asp:Content ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">   
  9.     <SharePoint:FieldValue id="PageTitle" FieldName="Title" runat="server"/>  
  10. </asp:Content>  
  11.   
  12. <asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">  
  13. </asp:Content>  
  14. <asp:Content ContentPlaceHolderId="PlaceHolderPageImage" runat="server"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></asp:Content>  
  15. <asp:Content ContentPlaceHolderId="PlaceHolderTitleBreadcrumb" runat="server"/>  
  16. <asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">  
  17. </asp:Content>  
  18. <asp:Content ContentPlaceHolderId="PlaceHolderBodyAreaClass" runat="server">  
  19. </asp:Content>  
  20.   
  21. <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">  
  22.   
  23.     <div class="zones">  
  24.   
  25.         <div>  
  26.             <WebPartPages:WebPartZone runat="server" AllowPersonalization="false" FrameType="TitleBarOnly" ID="WebPartZone1"   
  27.                                                 Title="TopZone" Orientation="Vertical"  
  28.                                                 QuickAdd-GroupNames="Default" QuickAdd-ShowListsAndLibraries="false"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone>  
  29.         </div>  
  30.         <div>  
  31.             <WebPartPages:WebPartZone runat="server" AllowPersonalization="false" FrameType="TitleBarOnly" ID="WebPartZone2"   
  32.                                                 Title="BottomZone" Orientation="Vertical"  
  33.                                                 QuickAdd-GroupNames="Default" QuickAdd-ShowListsAndLibraries="false"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone>  
  34.         </div>     
  35.                   
  36.     </div>  
  37.   
  38. </asp:Content>  

Add the page to the site as page layout

Add-SPOPublishingPageLayout command is used to create page layouts. The page layout should be associated with the content type. In this example, the article page content type is used for association. So we will first retrieve the content type id of article page and associate the content type to our new page layout.

The below code snippet helps in creating a page layout using PnP PowerShell.

  1. $pageCT = Get-SPOContentType -Identity "Article Page"  
  2. Write-Host $pageCT.Id  
  3. $pageLayout = Add-SPOPublishingPageLayout -SourceFilePath "D:\Articles\PnP\New Cmdlets\PageLayouts\TestLayout.aspx" -Title "TestLayout" -Description "Page Layout for testing PnP scripts" -DestinationFolderHierarchy "/" -AssociatedContentTypeID $pageCT.Id  

The below image shows the created page layout which is listed on the new page creation page. 


Retrieve Page Layouts

The page layouts can be retrieved using PnP commands. 

  • The get list operation (Get-SPOListItem) is used to retrieve the page layouts.
  • The necessary parameters for retrieving page layouts are list and query. List attribute will contain the list name and query parameter to filter out only page layouts from the master page gallery.

The following code snippet retrieves all page layouts from the site. The query used to filter only page layout is content type id. The page content type ids of all page layouts will begin with 0x01010007FF3E057FA8AB4AA42FCB67B453FFC1.

  1. # Retrieve all page layouts  
  2. $pageLayoutItems = Get-SPOListItem -List "Master Page Gallery" -Query "<View><Query><Where><BeginsWith><FieldRef Name='ContentTypeId'/><Value Type='ContentTypeId'>0x01010007FF3E057FA8AB4AA42FCB67B453FFC1</Value></BeginsWith></Where></Query></View>"  

The following code snippet retrieves the page layouts associated with article page content type.

  1. # Retrieve page layouts by content type  
  2. $pageLayoutItems = Get-SPOListItem -List "Master Page Gallery" -Query "<View><Query><Where><Contains><FieldRef Name='PublishingAssociatedContentType'/><Value Type='ContentTypeId'>0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D</Value></Contains></Where></Query></View>"  

The following code snippet retrieves the page layout by name.

  1. # Retrieve page layout by page layout name  
  2. $pageLayoutItem = Get-SPOListItem -List "Master Page Gallery" -Query "<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>TestLayout</Value></Contains></Where></Query></View>"  

The following command retrieves all the properties of page layout.

  1. $pageLayoutItem.FieldValues  

Delete Page Layout

The delete operation is carried out with delete list item command. To delete a page layout, first retrieve (Get-SPOListItem) the page layout item and then execute Remove-SPOListItem with the item identity.

The following code snippet helps in deleting a page layout.

  1. # Get page layout  
  2. $listItem = Get-SPOListItem -List "Master Page Gallery" -Query "<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>TestLayout</Value></Contains></Where></Query></View>"  
  3. # Delete page layout  
  4. Remove-SPOListItem -List "Master Page Gallery" -Identity $listItem  

The page layout can not be deleted when it is already referenced to a page.

The following image shows the error when deleted. The error is because of page layout reference. To get over this issue, you have to delete the referenced pages before executing the above operation.

  
Summary

Thus, you have learned how to create/add, retrieve or delete the page layouts on SharePoint site, using PnP PowerShell. PnP PowerShell is supported from SharePoint 2013 On Premise and Office 365 versions. The operations mentioned above, are tested on SharePoint 2013 and Office 365 environments.

Up Next
    Ebook Download
    View all
    Learn
    View all