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.
-
- string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
-
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- string title = "TestMasterPage";
-
-
- clientContext.Site.RootWeb.SetCustomMasterPageByName(title);
- Console.WriteLine("Custom Master Page appied by using master page name");
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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.
-
- string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
-
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- string path = "/sites/learning/_catalogs/masterpage/TestMasterPage.master";
-
- clientContext.Site.RootWeb.SetCustomMasterPageByUrl(path);
-
- Console.WriteLine("Master Page is applied to custom pages of SharePoint site");
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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.
-
- string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
-
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- string title = "TestMasterPage";
-
- clientContext.Site.RootWeb.SetMasterPageByName(title);
- Console.WriteLine("Master page is applied to system pages");
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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.
-
- string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
-
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- string path = "/sites/learning/_catalogs/masterpage/TestSystemMasterPage.master";
-
- clientContext.Site.RootWeb.SetMasterPageByUrl(path);
- Console.WriteLine("Master Page is applied to system pages");
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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.