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 = "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. // Custom & System Master Page names         

  14. string systemMaster = "TestSystemMasterPage.master";          

  15. string customMaster = "TestMasterPage.master";  

  16. // Applies master pages in single call         

  17. clientContext.Site.RootWeb.SetMasterPagesByName(systemMaster, customMaster);          

  18. Console.WriteLine("Updated System and Custom Master Pages");          

  19. Console.ReadKey();      

  20. }  

  21. }  

  22. catch (Exception ex)  

  23. {      

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

  25. Console.ReadKey();  

  26. }  

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 = "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. // System and Custom Master page paths         

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

  15. string customMasterPath = "/sites/learning/_catalogs/masterpage/TestMasterPage.master";  

  16. // Applies master pages         

  17. clientContext.Site.RootWeb.SetMasterPagesByUrl(systemMasterPath, customMasterPath);      

  18. Console.WriteLine("Updated System and Custom Master Pages");          

  19. Console.ReadKey();      

  20. }  

  21. }  

  22. catch (Exception ex)  

  23. {      

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

  25. Console.ReadKey();  

  26. }  


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. 

Recent Posts

See All