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):
- $siteurl = "https://abc.sharepoint.com"
- Connect-SPOnline -Url $siteurl
- $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.
- <%@ 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" %>
- <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- <%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
-
-
- <asp:Content ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
- <SharePoint:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
- </asp:Content>
-
- <asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
- </asp:Content>
- <asp:Content ContentPlaceHolderId="PlaceHolderPageImage" runat="server"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></asp:Content>
- <asp:Content ContentPlaceHolderId="PlaceHolderTitleBreadcrumb" runat="server"/>
- <asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
- </asp:Content>
- <asp:Content ContentPlaceHolderId="PlaceHolderBodyAreaClass" runat="server">
- </asp:Content>
-
- <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
-
- <div class="zones">
-
- <div>
- <WebPartPages:WebPartZone runat="server" AllowPersonalization="false" FrameType="TitleBarOnly" ID="WebPartZone1"
- Title="TopZone" Orientation="Vertical"
- QuickAdd-GroupNames="Default" QuickAdd-ShowListsAndLibraries="false"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone>
- </div>
- <div>
- <WebPartPages:WebPartZone runat="server" AllowPersonalization="false" FrameType="TitleBarOnly" ID="WebPartZone2"
- Title="BottomZone" Orientation="Vertical"
- QuickAdd-GroupNames="Default" QuickAdd-ShowListsAndLibraries="false"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone>
- </div>
-
- </div>
-
- </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.
- $pageCT = Get-SPOContentType -Identity "Article Page"
- Write-Host $pageCT.Id
- $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.
- # Retrieve all page layouts
- $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.
- # Retrieve page layouts by content type
- $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.
- # Retrieve page layout by page layout name
- $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.
- $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.
- # Get page layout
- $listItem = Get-SPOListItem -List "Master Page Gallery" -Query "<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>TestLayout</Value></Contains></Where></Query></View>"
- # Delete page layout
- 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.