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


Redjesh BehariPosted Aug 10, 2017, 2:11 PM
Hi Nakeeran, I have downloaded al the software but am unable to run commands like Connect-SPOnline or Get-SPOListItem am i missing something?
Vignesh ManiPosted Jul 11, 2016, 5:40 PM
Good one
Nanddeep NachanPosted Jul 11, 2016, 2:03 AM
Nice share