How to apply Master Pages Programmatically by Client Side Object Model – CSOM in SharePoint Office 365

Sathish Nadarajan
SharePoint MVP
Published On :   10 Sep 2016
Visit Count
Today :  2    Total :   5785

A small piece of code snippet to update the master pages of the Site in SharePoint Office 365 – Thought of sharing with the community.

The code is very straight forward and it can be reused as it is.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Microsoft.SharePoint.Client;
 using System.Security;
 using Microsoft.Online.SharePoint.TenantAdministration;
 using OfficeDevPnP.Core;
 using Microsoft.SharePoint.Client.Publishing;
 namespace Console.Office365
     class Program
         static void Main(string[] args)
         public static void ApplyMasterPage()
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
             string siteUrl = "https://********";
             string userName = "sathish@******";
             string password = "*******";
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                 Web web = ctx.Web;
                 web.MasterUrl = web.ServerRelativeUrl + "/_catalogs/masterpage/oslo.master";
                 web.CustomMasterUrl = web.ServerRelativeUrl + "/_catalogs/masterpage/oslo.master";

Happy Coding,

Sathish Nadarajan.