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.
- // Input Parameters
- string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";
- string userName = "[email protected]";
- string password = "***";
- // PnP component to set context
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
- // Get and set the client context
- // Connects to SharePoint online site using inputs provided
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
- // Master Page Name
- string title = "TestMasterPage";
- // Apply Master Page
- 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();
- }
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.
- // Input Parameters
- string siteUrl = "https://nakkeerann.sharepoint.com/sites/learning";
- string userName = "[email protected]";
- string password = "***";
- // PnP component to set context
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
- // Get and set the client context
- // Connects to SharePoint online site using inputs provided
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
- // Master Page Path
- string path = "/sites/learning/_catalogs/masterpage/TestMasterPage.master";
- // Applies master page to custom pages
- 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();
- }

Humayun Kabir MamunPosted Oct 6, 2016, 12:14 AM
Nice...