Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.....

share|improve this question
    
try using server relative url for master page as well –  Amit Kumawat Mar 18 '13 at 9:41
    
Thanks @AmitKumawat , but it didn't help me either....I tried this earlier as well... –  nks Mar 18 '13 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 '13 at 7:53
add comment

2 Answers

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());

    });
}
share|improve this answer
add comment

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();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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