2

I have created a Site Collection in SharePoint 2013 online.

In this site I am trying to update Master Page through JavaScript Client Object Model. My JQuery scripts are able to change the Site Master page as well as System Master Page. I can validate the change under Site Settings  Master Page (under Look and Feel section).

I am able to execute the script successfully without any error. But when I refresh my home page, it redirects me to error page saying “File Not Found” Error and it gives one correlation ID. When I change any setting in Master Page and save it, my site again looks good.

code which I am using:

//context = new SP.ClientContext.get_current();
//var site = clientContext.get_site();
//var web = context.get_site();

context = new SP.ClientContext.get_current();
var web = context.get_site().get_rootWeb();
context.load(web, 'ServerRelativeUrl');
web.set_customMasterUrl('/_catalogs/masterpage/seattle.master');
web.set_masterUrl('/_catalogs/masterpage/seattle.master');
web.update();

I tried with commented code also for trial... Would appreciate if someone could give me some direction here.....

3
  • try using server relative url for master page as well Mar 18, 2013 at 9:41
  • Thanks @AmitKumawat , but it didn't help me either....I tried this earlier as well...
    – nks
    Mar 18, 2013 at 20:14
  • ok, I hardcoded the ServerRelativeUrl and it worked for me, but I cannot do this in real time. I have to have the serverrelativeurl before setting master page.. web.set_customMasterUrl('/teams/mysite_abc/_catalogs/masterpage/seattle.master');
    – nks
    Mar 19, 2013 at 7:53

2 Answers 2

1

Perfectly working Code:

$(document).ready(function () {
    jQuery('.cmdSet').click(function () {

        var scriptBase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";

        $.getScript(scriptBase + "sp.runtime.js", function () {

            $.getScript(scriptBase + "sp.js", function () {

                $.getScript(scriptBase + "sp.core.js", sharePointReady);

            });
        });

    });
});



    // create page-level variables to hold client context and web
    var context;
    var web;
    var masterurl;
    var site;
    function sharePointReady() {

    // assign values to page-level variables
    context = new SP.ClientContext.get_current();
    web = context.get_web();

    // provide CSOM with instructions to load info about current web


    context.load(web, 'ServerRelativeUrl');
    web.set_customMasterUrl(L_Menu_BaseUrl + '/_catalogs/masterpage/seattle.master');
    web.set_masterUrl(L_Menu_BaseUrl + '/_catalogs/masterpage/seattle.master');
    web.update();

    context.executeQueryAsync(function () {

        alert("Starting Master Page Setting......");
        masterurl = web.get_serverRelativeUrl() + "/_catalogs/masterpage/seattle.master";
        alert(masterurl);
        alert("Master Page is Set Successfully!!!");

    }, function (sender, args) {

        alert("Error: " + args.get_message());

    });
}
0

Get your server relative url using _spPageContextInfo

var customMasterPage = "custom.v15.master";
var siteRelativeUrl = _spPageContextInfo.siteServerRelativeUrl;

web.set_customMasterUrl(siteRelativeUrl + '/_catalogs/masterpage/' + customMasterPage);
web.set_masterUrl(siteRelativeUrl + '/_catalogs/masterpage/' + customMasterPage);
web.update();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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