Deploying Page Layouts On SharePoint Using PnP Core CSOM

Introduction

In this article, you will learn how to deploy a page layout, using PnP Client Side Object Model code.

The page layout can be created manually in many ways. The page layouts can be deployed manually or using the deployment tools built , using programming languages. Here, let us see how the page layouts can be deployed, using PnP CSOM programming language.

If you are new to PnP programming, please refer to the ebook on PnP CSOM programming, which has the introduction, basics and samples on PnP CSOM programming.

Prerequisites
  • Microsoft Visual Studio
  • SharePoint Online portal

Page layouts

Page layouts can be of multiple types. They are article pages, publishing pages, welcome pages etc. Each of the page layouts are associated with a content type. All the page layouts are differentiated, using content type id. The screen shot given below shows the page of the available content types (Content types page - /_layouts/15/mngctype.aspx).

 

Custom page layouts can be built, using the existing content types. These existing content types are called as associated content types, while creating a custom page layout.

Steps involved

Create a page layout manually, using the editors or modify the existing page layout by downloading from the site.

Create a new console Application and install the packages required for PnP CSOM programming, using NuGet Package Manager. (The detailed steps to create a project has been explained in the ebook mentioned above).

Create the layout, using DeployPageLayout command. The required input parameters are source file path, title, description, associate content type id.

The content type ID of the page layouts can be identified from the content type page (/_layouts/15/mngctype.aspx). For example, to get the content type ID of article page layout, click the article page content type. The ID can be extracted from the URL (ctype parameter).

 

In the sample given below, you can find the PnP CSOM code snippet to deploy the custom publishing page layout.

  1. AuthenticationManager authManager = new AuthenticationManager();  
  2. try  
  3. {  
  4.     // Get and set the client context   
  5.     // Connects to SharePoint online site using inputs provided   
  6.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  7.     {  
  8.         string sourceFilePath = @"D:\Workspace\Nakkeeran\CustomArticleLayout.aspx";  
  9.         string title = "CustomArticleLayout";  
  10.         string description = "Layout for custom article pages";  
  11.         string pubpagelayoutCID = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D";  
  12.         clientContext.Web.DeployPageLayout(sourceFilePath, title, description, pubpagelayoutCID);  
  13.         Console.WriteLine("Page Layout successfuly deployed");  
  14.         Console.ReadKey();  
  15.     }  
  16.   
  17. }  
  18. catch (Exception ex)  
  19. {  
  20.     Console.WriteLine("Error Message: " + ex.Message);  
  21.     Console.ReadKey();  
  22. }   

Deploy/ Run

From the console Application, run the code by pressing F5 and you can see the success/error details on the command prompt.

The snapshot given below (Page creation screen) shows the custom article page layout created, using PnP Core CSOM programming, which is ready for use. The pages can be created, using the layout. Likewise, other page layouts can also be created.

 
Summary

Thus, you have seen the basics and steps to deploy the page layout, using PnP core CSOM programming.

Next Recommended Readings