SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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

    context.executeQueryAsync(function () {

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

    }, function (sender, args) {

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

share|improve this answer

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

Your Answer


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.