Working With SharePoint Online Master Pages - Part Three

Introduction
 
In this article, you will learn how to apply both System and Custom Master Pages, to the SharePoint online sites, using PnP Core Client Side Object Model library, in single execution.
 
This article is part three of SharePoint Online Master Page operations article series. In my previous articles, you learned deploying Master Pages, retrieving the Master Page path, and applying Master Pages.
In this article, you will learn about the below operations.
  • Apply Custom and System Master Page by Name
  • Apply Custom and System Master Page by URL
Apply Custom/System Master Page
 
Both System master page and Custom master page can be applied to SharePoint Online site, using single method execution. The same operation can be performed using two different methods.
 
By System & Custom Master Page Names
 
Here, System and Custom master pages are applied to the sites, using master page names.
 
The steps involved are,
  • Input the site detail, user details for authentication, and the master page name (system & custom).
  • Authenticate and get the client context of the site using authentication manager.
  • Apply the master page using SetMasterPagesByName method with the help of root web object. The input parameters required are custom and system master page names/titles.
  • Display results on the console. 
The code snippet given below shows the custom and system master page update operation, using master page name.
  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.         // Custom & System Master Page names  
  16.         string systemMaster = "TestSystemMasterPage.master";  
  17.         string customMaster = "TestMasterPage.master";  
  18.         // Applies master pages in single call  
  19.         clientContext.Site.RootWeb.SetMasterPagesByName(systemMaster, customMaster);  
  20.         Console.WriteLine("Updated System and Custom Master Pages");  
  21.         Console.ReadKey();  
  22.     }  
  23. }  
  24. catch (Exception ex)  
  25. {  
  26.     Console.WriteLine("Error Message: " + ex.Message);  
  27.     Console.ReadKey();  
  28. }   
By System & Custom Master Page Urls
 
Here, System & Custom master pages are applied to the sites, using master page server relative paths.
 
The steps involved are,
  • Input the site detail, user details for authentication and the master page (system & custom) server relative paths.
  • Authenticate and get the client context of the site using authentication manager.
  • Apply the master page using SetMasterPagesByUrl method with the help of root web object. The input parameters required are custom and system master page server relative paths.
  • Display results on the console. 
The code snippet given below shows the custom and system master page update operation using master page server relative paths.
  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.         // System and Custom Master page paths  
  16.         string systemMasterPath = "/sites/learning/_catalogs/masterpage/TestSystemMasterPage.master";  
  17.         string customMasterPath = "/sites/learning/_catalogs/masterpage/TestMasterPage.master";  
  18.         // Applies master pages  
  19.         clientContext.Site.RootWeb.SetMasterPagesByUrl(systemMasterPath, customMasterPath);  
  20.         Console.WriteLine("Updated System and Custom Master Pages");  
  21.         Console.ReadKey();  
  22.     }  
  23. }  
  24. catch (Exception ex)  
  25. {  
  26.     Console.WriteLine("Error Message: " + ex.Message);  
  27.     Console.ReadKey();  
  28. }  
Summary
 
Thus, you have learned updating System and Custom Master Pages at a time by various methods.
 
Thus, in this article series, you have learned the SharePoint Online basic Master Page operations, using PnP Core Client Side Object Model library.