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;
web.Update();
clientContext.Load(web);
clientContext.ExecuteQuery();

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);
clientContext.Load(newfile);
clientContext.ExecuteQuery();

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s