2

I have the following code to assign masterpage via CSOM but its not working, after I go to the site, it says File not Found, I guess URL is wrongly constructed.

Please note that I am not trying to set master page to root web, its subsite in a nested structrue

/rootweb
/rootweb/clients/c1
/rootweb/clients/c2

The only thing I can think of, is that the master page url should be relative to the rootweb of the site collection and not the subsite??

private static void BrandingSubsite(ClientContext clientContext, Web newWeb)
        {
            clientContext.Load(newWeb, w => w.ServerRelativeUrl);
            clientContext.ExecuteQuery();

            var masterPageUrl = String.Format("{0}/_catalogs/masterpage/SPCCapatech/FullWidth_SideBar.master", newWeb.ServerRelativeUrl);

            newWeb.MasterUrl = masterPageUrl;
            newWeb.CustomMasterUrl = masterPageUrl;
            newWeb.Update();
            clientContext.Load(newWeb);
            clientContext.ExecuteQuery();           
        }
4

Master page gallery is present in the root web of the site collection. So, whether a master page is applied to a root site or any sub site, it is the server relative url of the root site collection which is needed. Consider that you have a sub site under the root site collection: http://aissp2013/sites/pub/s1. Here is the code to apply master page to it:

using (var ctx = new ClientContext("http://aissp2013/sites/pub/s1"))
{
    //Get root site web
    Web rootWeb = ctx.Site.RootWeb;
    ctx.Load(rootWeb, w => w.ServerRelativeUrl);
    ctx.ExecuteQuery();
    string masterPageUrl = rootWeb.ServerRelativeUrl + "/_catalogs/masterpage/SPCCapatech/FullWidth_SideBar.master";

    //Get sub web
    Web subWeb = ctx.Web;
    subWeb.CustomMasterUrl = masterPageUrl;
    subWeb.MasterUrl = masterPageUrl;
    subWeb.Update();
    ctx.Load(subWeb);
    ctx.ExecuteQuery();
}

Note that whatever be the level of sub site, same code will be used to get hold of master page i.e. getting server relative url of root site. Only the web object (subsite) will be different.

1
  • 1
    You should probably enhance this (for root site collection) rootWeb.ServerRelativeUrl to rootWeb.ServerRelativeUrl.TrimEnd('/') to avoid getting double / – Robert Lindgren Aug 21 '15 at 7:22
2
using (var ctx = new ClientContext( url ))
{
    var web = ctx.Web;
    var masterPageUrl = String.Format( "{0}/_catalogs/masterpage/SPCCapatech/FullWidth_SideBar.master", ctx.Web.ServerRelativeUrl );

    web.MasterUrl = masterPageUrl;
    web.CustomMasterUrl = masterPageUrl;
    web.Update();

    ctx.Load( web );
    ctx.ExecuteQuery();
}

http://www.sharepointsol.com/2015/01/apply-master-page-using-csom.html

<script>
        function MastePageChange() {
            var ctx;
            var web;
            var customMasterURL = '/_catalogs/masterpage/SPCCapatech/FullWidth_SideBar.master';
            ctx = new SP.ClientContext.get_current();
            web = ctx.get_web();
            web.set_customMasterUrl(masterPageUrl);
            web.set_masterUrl(masterPageUrl);
            web.update();
            ctx.executeQueryAsync(function () {
               alert("Master Page has been changed successfully \n" + customMasterURL);
            }, function (sender, args) {
                alert("Error: " + args.get_message());
            });
        }
       MastePageChange();
    </script>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.