Set Custom Master Page through CSOM SharePoint

Compare to SharePoint server-side object model, you need to set an absolute URL to the MasterUrl and CustomMasterUrl

string masterUrl = String.Format(“{0}/_catalogs/masterpage/my.master”, clientContext.Site.ServerRelativeUrl);

web.MasterUrl = masterUrl;
web.CustomMasterUrl = masterUrl;

Before change the master page url, you have to upload the master page to “Master Page Gallery” of Site.

string masterPagePath = Server.MapPath(“cibcmysite15.master”);  // provider-hosted app – remote web application

List mpGallery = clientContext.Site.RootWeb.Lists.GetByTitle(“Master Page Gallery”);
FileCreationInformation fci = new FileCreationInformation();
fci.Content = System.IO.File.ReadAllBytes(masterPagePath);
fci.Url = “my.master”;
fci.Overwrite = true;

Microsoft.SharePoint.Client.File newfile = mpGallery.RootFolder.Files.Add(fci);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s