0

I am using SharePoint 2013 Online and I'm looking for some JavaScript and html to get current master page for the site and change the same with other master page.

If anyone could help or just point myself in the correct direction it will be a help. Thanks in advance

1

To get current master page, make a GET request to following end-point

/_api/Web?$select=CustomMasterUrl,MasterUrl

To change current master page, see here.

function MastePageChange() {
    var clientcontext;
    var cweb;
    var customMasterURL = '/_catalogs/masterpage/NewMasterPage.master';
    clientcontext = new SP.ClientContext.get_current();
    cweb = clientcontext.get_web();
    cweb.set_customMasterUrl(masterPageUrl);
    cweb.set_masterUrl(masterPageUrl);
    cweb.update();
    clientcontext.executeQueryAsync(function() {
        alert("Master Page has been changed successfully \n" + customMasterURL);
    }, function(sender, args) {
        alert("Error: " + args.get_message());
    });
}

MastePageChange();
  • The code to change current master page provided on your link is working first time only but on page refresh it gives me an error that "File not found" . – Mrunal Shidurkar Mar 28 '16 at 6:17
  • Did u include .master with file name. I mean url should look like /_catalogs/masterpage/NewMasterPage.master. See the updated code – Atish Dipongkor - MVP Mar 28 '16 at 6:22
  • yes i have changeed it. for the first time the code works fine. but on page refresh it gives an error – Mrunal Shidurkar Mar 28 '16 at 6:26
  • and cweb.set_customMasterUrl(masterPageUrl); this also gives me an error that masterPageUrl not initialsize so I replace it with customMasterURL – Mrunal Shidurkar Mar 28 '16 at 6:28
  • It should work. I am using in my project. Check the master page url again. – Atish Dipongkor - MVP Mar 28 '16 at 15:34

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.