top of page
Writer's pictureNakkeeran Natarajan

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. <asp:Content ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">

  7. <SharePoint:FieldValue id="PageTitle" FieldName="Title" runat="server"/>

  8. </asp:Content>

  9. <asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">

  10. </asp:Content> <asp:Content ContentPlaceHolderId="PlaceHolderPageImage" runat="server">

  11. <IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt="">

  12. </asp:Content>

  13. <asp:Content ContentPlaceHolderId="PlaceHolderTitleBreadcrumb" runat="server"/>

  14. <asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

  15. </asp:Content>

  16. <asp:Content ContentPlaceHolderId="PlaceHolderBodyAreaClass" runat="server">

  17. </asp:Content>

  18. <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">

  19. <div class="zones">

  20. <div>

  21. <WebPartPages:WebPartZone runat="server" AllowPersonalization="false" FrameType="TitleBarOnly" ID="WebPartZone1" Title="TopZone" Orientation="Vertical" QuickAdd-GroupNames="Default" QuickAdd-ShowListsAndLibraries="false">

  22. <ZoneTemplate>

  23. </ZoneTemplate>

  24. </WebPartPages:WebPartZone>

  25. </div>

  26. <div>

  27. <WebPartPages:WebPartZone runat="server" AllowPersonalization="false" FrameType="TitleBarOnly" ID="WebPartZone2" Title="BottomZone" Orientation="Vertical" QuickAdd-GroupNames="Default" QuickAdd-ShowListsAndLibraries="false">

  28. <ZoneTemplate>

  29. </ZoneTemplate>

  30. </WebPartPages:WebPartZone>

  31. </div>

  32. </div>

  33. </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>

  3. <Where><BeginsWith><FieldRef Name='ContentTypeId'/>

  4. <Value Type='ContentTypeId'>0x01010007FF3E057FA8AB4AA42FCB67B453FFC1</Value>

  5. </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>

  3. <Where><Contains><FieldRef Name='PublishingAssociatedContentType'/>

  4. <Value Type='ContentTypeId'>0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D</Value><

  5. /Contains>

  6. </Where>

  7. </Query>

  8. </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>

  3. <Where><Contains><FieldRef Name='Title'/><Value Type='Text'>TestLayout</Value></Contains></Where>

  4. </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.

0 comments

Comments


bottom of page