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 = "nav@nakkeerann.onmicrosoft.com"; 

  4.  string password = "***";  

  5. // PnP component to set context  

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context  

  10. // Connects to SharePoint online site using inputs provided  

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // Input parameters for uploading master page         

  14. string path = @"C:\Solution\PnPCSOM\MasterPageOperations\TestMasterPage.master";          

  15. string title = "TestMasterPage";          

  16. string description = "Updated by PnP";          

  17. clientContext.Site.RootWeb.DeployMasterPage(path, title, description, "15", "", "/");          

  18. Console.WriteLine("Master Page uploaded to the site's master page gallery!");          

  19. Console.ReadKey();      

  20. }  

  21.  catch (Exception ex) 

  22.  {      

  23. Console.WriteLine("Error Message: " + ex.Message);      

  24. Console.ReadKey();  

  25. }  

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 = "nav@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context  

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context  

  10. // Connects to SharePoint online site using inputs provided  

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {          

  13. string title = "TestMasterPage";          

  14. string path = clientContext.Site.RootWeb.GetRelativeUrlForMasterByName(title);          

  15. Console.WriteLine("Master Page URL (available on master page gallery): " + path);          

  16. Console.ReadKey();      

  17. }  

  18. }  

  19. catch (Exception ex)  

  20. {      

  21. Console.WriteLine("Error Message: " + ex.Message);      

  22. Console.ReadKey();  

  23. }  

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.

Recent Posts

See All