PnP core CSOM documentation can be found on Office site
here. PnP Core CSOM library packages can be downloaded
here. The code, given below, is being tested, using Visual Studio Console Application. Once the Console Application is created, the packages can be installed, using Install-Package SharePointPnPCoreOnline command on Package Manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.
The references used in the sample are given below-
- Microsoft.SharePoint.Client
- OfficeDevPnP.Core
Connect to SharePoint online site
The authentication manager is used to retrieve the client context of the site. To connect to SharePoint online site, the method, given below, is used-
- GetSharePointOnlineAuthenticatedContextToken
The parameters required are-
- SharePoint online site URL
- Tenant UserId
- Tenant Password (or secured string)
Note:
- The master pages can be uploaded/viewed from the master pages gallery.
- The master pages can be applied manually from Master page settings page.
- The master pages should be available on the master page gallery before executing these operations.
Upload/Deploy Master Page
The master page can be uploaded to SharePoint Online sites, using PnP Core CSOM library. The steps involved are-
- Input the site detail, user details for authentication and the local master page path.
- Authenticate and get the client context of the site, using authentication manager.
- Upload the master page, using DeployMasterPage method with the help of root Web object. The required input parameters are master page local system path, master page title, description, UI version, default css file and folder path (root folder or subfolder of master page gallery).
- Display the results on the console.
The code snippet given below shows the upload/deploy operation-
-
- 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 = @"C:\Solution\PnPCSOM\MasterPageOperations\TestMasterPage.master";
- string title = "TestMasterPage";
- string description = "Updated by PnP";
- clientContext.Site.RootWeb.DeployMasterPage(path,title, description, "15", "", "/");
- Console.WriteLine("Master Page uploaded to the site's master page gallery!");
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
Retrieve Master Page Url
The master pages available on the portal can be retrieved using PnP Core CSOM library. The steps involved are,
- Input the site detail, user details for the authentication and the master page name.
- Authenticate and get the client context of the site, using authentication manager.
- Retrieve the master page URL, using GetRelativeUrlForMasterByName method with the help of root Web object. The required input parameter is master page title.
- Display the results on the console.
The code snippet, given below shows retrieving the 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 title = "TestMasterPage";
- string path = clientContext.Site.RootWeb.GetRelativeUrlForMasterByName(title);
- Console.WriteLine("Master Page URL (available on master page gallery): " + path);
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
Summary
Thus, you have learned how to deploy the master page to the site and retrieve the Server related URL of the master pages, using PnP Core CSOM library.
In the next article, you will learn about other basic master page operations in detail.