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 = "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. // Master Page Name         

  14. string title = "TestMasterPage";  

  15. // Apply Master Page         

  16. clientContext.Site.RootWeb.SetCustomMasterPageByName(title);          

  17. Console.WriteLine("Custom Master Page applied by using master page name");          

  18. Console.ReadKey();      

  19. }  

  20. }  

  21. catch (Exception ex)  

  22. {      

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

  24. Console.ReadKey();  

  25. }  

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 = "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. // Master Page Path         

  14. string path = "/sites/learning/_catalogs/masterpage/TestMasterPage.master";  

  15. // Applies master page to custom pages         

  16. clientContext.Site.RootWeb.SetCustomMasterPageByUrl(path);           

  17. Console.WriteLine("Master Page is applied to custom pages of SharePoint site");          

  18. Console.ReadKey();      

  19. }  

  20. }  

  21. catch (Exception ex)  

  22. {      

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

  24. Console.ReadKey();  

  25. }  

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 = "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. // Master Page title/name         

  14. string title = "TestMasterPage";  

  15. // Applies master page to system pages         

  16. clientContext.Site.RootWeb.SetMasterPageByName(title);          

  17. Console.WriteLine("Master page is applied to system pages");          

  18. Console.ReadKey();      

  19. }  

  20. }  

  21. catch (Exception ex)  

  22. {      

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

  24. Console.ReadKey();  

  25. }  


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 = "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. // Master Page Path         

  14. string path = "/sites/learning/_catalogs/masterpage/TestSystemMasterPage.master";  

  15. // Apply Master Page by Url         

  16. clientContext.Site.RootWeb.SetMasterPageByUrl(path);          

  17. Console.WriteLine("Master Page is applied to system pages");          

  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 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.

Recent Posts

See All