Working With SharePoint Online Master Pages Using PnP Core CSOM Library - Part One

Introduction
 
In this article series, you will learn the basic SharePoint online master page operations, which can be executed, using PnP Core Client Side Object Model library. 
 
This article is a part one of SharePoint online master page operations article series.
 
In this article, you will learn the operations, shown below-
  • Upload/Deploy Master Page
  • Get Master Page Url 
Prerequisite
 
PnP core CSOM documentation can be found on Office site here. PnP Core CSOM library packages can be downloaded here. The code, given below, is being tested, using Visual Studio Console Application. Once the Console Application is created, the packages can be installed, using Install-Package SharePointPnPCoreOnline command on Package Manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.
 
The references used in the sample are given below-
  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core
Connect to SharePoint online site
 
The authentication manager is used to retrieve the client context of the site. To connect to SharePoint online site, the method, given below, is used-
  • GetSharePointOnlineAuthenticatedContextToken 
The parameters required are-
  • SharePoint online site URL
  • Tenant UserId
  • Tenant Password (or secured string)
Note
  • The master pages can be uploaded/viewed from the master pages gallery.
  • The master pages can be applied manually from Master page settings page.
  • The master pages should be available on the master page gallery before executing these operations. 
Upload/Deploy Master Page
 
The master page can be uploaded to SharePoint Online sites, using PnP Core CSOM library. The steps involved are-
  • Input the site detail, user details for authentication and the local master page path.
  • Authenticate and get the client context of the site, using authentication manager.
  • Upload the master page, using DeployMasterPage method with the help of root Web object. The required input parameters are master page local system path, master page title, description, UI version, default css file and folder path (root folder or subfolder of master page gallery).
  • Display the results on the console. 
The code snippet given below shows the upload/deploy operation-
  1. // Input Parameters    
  2. string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context    
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8.   
  9. try  
  10. {  
  11.     // Get and set the client context    
  12.     // Connects to SharePoint online site using inputs provided    
  13.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  14.     {  
  15.         // Input parameters for uploading master page  
  16.         string path = @"C:\Solution\PnPCSOM\MasterPageOperations\TestMasterPage.master";  
  17.         string title = "TestMasterPage";  
  18.         string description = "Updated by PnP";  
  19.         clientContext.Site.RootWeb.DeployMasterPage(path,title, description, "15""""/");  
  20.         Console.WriteLine("Master Page uploaded to the site's master page gallery!");  
  21.         Console.ReadKey();  
  22.     }  
  23. }  
  24. catch (Exception ex)  
  25. {  
  26.     Console.WriteLine("Error Message: " + ex.Message);  
  27.     Console.ReadKey();  
  28. }   
Retrieve Master Page Url 
 
The master pages available on the portal can be retrieved using PnP Core CSOM library. The steps involved are,
  • Input the site detail, user details for the authentication and the master page name.
  • Authenticate and get the client context of the site, using authentication manager.
  • Retrieve the master page URL, using GetRelativeUrlForMasterByName method with the help of root Web object. The required input parameter is master page title.
  • Display the results on the console.
The code snippet, given below shows retrieving the master page URL-
  1. // Input Parameters    
  2. string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context    
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8.   
  9. try  
  10. {  
  11.     // Get and set the client context    
  12.     // Connects to SharePoint online site using inputs provided    
  13.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  14.     {  
  15.         string title = "TestMasterPage";  
  16.         string path = clientContext.Site.RootWeb.GetRelativeUrlForMasterByName(title);  
  17.         Console.WriteLine("Master Page URL (available on master page gallery): " + path);  
  18.         Console.ReadKey();  
  19.     }  
  20. }  
  21. catch (Exception ex)  
  22. {  
  23.     Console.WriteLine("Error Message: " + ex.Message);  
  24.     Console.ReadKey();  
  25. }   
Summary
 
Thus, you have learned how to deploy the master page to the site and retrieve the Server related URL of the master pages, using PnP Core CSOM library.
 
In the next article, you will learn about other basic master page operations in detail.

Up Next
    Ebook Download
    View all
    Learn
    View all