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

Introduction
 
In this article, you will learn how to apply available Master Pages to the SharePoint Online sites, using PnP Core Client Side Object Model library.
 
This article is part two of SharePoint Online Master Page operations article series. In my previous article, you would have seen deploying master page and retrieving the master page path. 
In this article, you will learn about the below operations.
  • Apply Custom Master Page by Name
  • Apply Custom Master Page by URL
  • Apply System Master Page by Name
  • Apply System Master Page by URL
Apply Custom Master Page
 
The custom master pages can be applied to SharePoint online sites by two different methods, using PnP Core CSOM library.
 
By Master Page Name 
 
The custom master pages can be applied to the SharePoint online site using custom master page name (available on master pages gallery) with corresponding PnP Core CSOM library method. 
 
The steps involved are,
  • Input the site detail, user details for authentication and the custom master page name (from master page gallery).
  • Authenticate and get the client context of the site using authentication manager.
  • Apply the custom master page using SetCustomMasterPageByName method with the help of root web object. The required input parameter is master page name/title.
  • Display the results on the console.
The code snippet given below shows the custom 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.         // Master Page Name  
  16.         string title = "TestMasterPage";  
  17.   
  18.         // Apply Master Page  
  19.         clientContext.Site.RootWeb.SetCustomMasterPageByName(title);  
  20.         Console.WriteLine("Custom Master Page appied by using master page name");  
  21.         Console.ReadKey();  
  22.     }  
  23. }  
  24. catch (Exception ex)  
  25. {  
  26.     Console.WriteLine("Error Message: " + ex.Message);  
  27.     Console.ReadKey();  
  28. }  
By Master Page URL
 
The custom master pages can be applied to the SharePoint online site separately using master page Server relative URL (available on master pages gallery) with corresponding PnP Core CSOM library method.
 
The steps involved are,
  • Input the site detail, user details for authentication and the master page server relative URL.
  • Authenticate and get the client context of the site using authentication manager.
  • Apply the master page using "SetCustomMasterPageByUrl" method with the help of root web object. The required input parameter is server relative URL of master page to be applied.
  • Display results or check the pages for change.
The code snippet given below shows the custom master page update operation using 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.         // Master Page Path  
  16.         string path = "/sites/learning/_catalogs/masterpage/TestMasterPage.master";  
  17.         // Applies master page to custom pages  
  18.         clientContext.Site.RootWeb.SetCustomMasterPageByUrl(path);  
  19.   
  20.         Console.WriteLine("Master Page is applied to custom pages of SharePoint site");  
  21.         Console.ReadKey();  
  22.     }  
  23. }  
  24. catch (Exception ex)  
  25. {  
  26.     Console.WriteLine("Error Message: " + ex.Message);  
  27.     Console.ReadKey();  
  28. }  
Apply System Master Page
 
The system master pages can be applied by two different methods.
 
By Master Page Name
 
The system master pages can be applied to the SharePoint online site separately using master page name with corresponding PnP Core method.
 
The steps involved are, 
  • Input the site detail, user details for authentication and the master page name.
  • Authenticate and get the client context of the site using authentication manager.
  • Apply the master page using SetMasterPageByName method with the help of root web object. The required input parameter is master page title/name.
  • Display results on console. 
The code snippet given below shows the 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.         // Master Page title/name  
  16.         string title = "TestMasterPage";  
  17.         // Applies master page to system pages  
  18.         clientContext.Site.RootWeb.SetMasterPageByName(title);  
  19.         Console.WriteLine("Master page is applied to system pages");  
  20.         Console.ReadKey();  
  21.     }  
  22. }  
  23. catch (Exception ex)  
  24. {  
  25.     Console.WriteLine("Error Message: " + ex.Message);  
  26.     Console.ReadKey();  
  27. }  
By Master Page Url 
 
The system master pages can be applied to the SharePoint online site separately using master page Server relative URL with corresponding PnP Core CSOM library method.
 
The steps involved are,
  • Input the site detail, user details for authentication and the master page server relative URL.
  • Authenticate and get the client context of the site using authentication manager.
  • Apply the master page using "SetMasterPageByUrl" method with the help of root web object. The required input parameter is server relative URL of master page.
  • Display results or check the pages for change. 
The code snippet given below shows the system master page update operation using 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.         // Master Page Path  
  16.         string path = "/sites/learning/_catalogs/masterpage/TestSystemMasterPage.master";  
  17.         // Apply Master Page by Url  
  18.         clientContext.Site.RootWeb.SetMasterPageByUrl(path);  
  19.         Console.WriteLine("Master Page is applied to system pages");  
  20.         Console.ReadKey();  
  21.     }  
  22. }  
  23. catch (Exception ex)  
  24. {  
  25.     Console.WriteLine("Error Message: " + ex.Message);  
  26.     Console.ReadKey();  
  27. }   
Summary
 
Thus you have learned how to apply custom or system master pages individually to the SharePoint Online sites, using various methods with the help of PnP Core CSOM library.
 
In the next article, you will learn how both, custom and system master pages, can be applied to the SharePoint Online sites at a time.

Up Next
    Ebook Download
    View all
    Learn
    View all