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.
-
- 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 systemMaster = "TestSystemMasterPage.master";
- string customMaster = "TestMasterPage.master";
-
- clientContext.Site.RootWeb.SetMasterPagesByName(systemMaster, customMaster);
- Console.WriteLine("Updated System and Custom Master Pages");
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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.
-
- 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 systemMasterPath = "/sites/learning/_catalogs/masterpage/TestSystemMasterPage.master";
- string customMasterPath = "/sites/learning/_catalogs/masterpage/TestMasterPage.master";
-
- clientContext.Site.RootWeb.SetMasterPagesByUrl(systemMasterPath, customMasterPath);
- Console.WriteLine("Updated System and Custom Master Pages");
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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.