Skip to main content

Change SharePoint/O365 MasterPage using JSOM

Here is one way to change the master page of a SharePoint site using JSOM, I have tested this in O365 site but it should work with on-prem version of SharePoint as well.

This code can be run on a script editor webpart and it works on the click of a html button control.

<script src="/_layouts/15/SP.Runtime.js" type="text/javascript"></script>
<script src="/_layouts/15/SP.js" type="text/javascript"></script>

<script type="text/javascript">

function ChangeMasterPage()
{
var customMasterPage = "/_catalogs/masterpage/Custom.master";
var siteRelativeUrl = _spPageContextInfo.siteServerRelativeUrl;
var clientContext;
var currentWeb;

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
clientContext = new SP.ClientContext.get_current();
currentWeb = clientContext.get_web();

currentWeb.set_customMasterUrl(siteRelativeUrl + customMasterPage);
currentWeb.set_masterUrl(siteRelativeUrl + customMasterPage);
currentWeb.update();

clientContext.load(currentWeb);

clientContext.executeQueryAsync(function () {
alert('MasterPage is changed successfully.');
        window.location.reload();            
    }, function (sender, args) {
        alert("Error: " + args.get_message());
    });

});
}
</script> ​​​​ ​​​

<input type="button" style="height:50px; width:200px; color:red;" value="Click Me" onclick="ChangeMasterPage();" />

Comments

Popular posts from this blog

Users do not show up in SharePoint People Search or People Picker

I had this issue with people picker in a classic mode web application in SharePoint 2013 and this site is in 2010 mode - users in certain sub-domains would not show up in People Picker. I was aware of stsadm commands to fix this and we ran the stsadm command to hook up people picker with another domain some time back. The latest issue was that people picker was not returning users from the root domain and few sub domains. After researching on the internet I found (contrary to my thoughts) that we could use PowerShell and not just stsadm to map People Picker to domains. It is a good idea to first check what domains are added/mapped to the web application using the following commands: $wa = Get-SPWebApplication -Identity http://mywebapp.com #List the Domains $wa.PeoplePickerSettings.SearchActiveDirectoryDomains This will list the domains currently People Picker is looking up for that web application. I used the following script to map our AD forest to People Picker: ...

"Cannot impersonate user for data source" - SSRS reports

We were getting this strange error while viewing our reports in SharePoint that was deployed using BIDS 2005: An error has occurred during report processing. Cannot impersonate user for data source 'datasource' Logon failed I tried many things but nothing worked. I did some research and found many blog posts which suggested me to reapply the password on the datasource file specified in the error message and save it again. That didn't work for me. I saw some check boxes in the data source file that read "Use as Windows Credentials" and "Set Execution Context to this report" and I didn't know what they meant. I unchecked those in my data source and then when the refreshed the page the report started displaying.

Page layout HTML changes not reflected in associated aspx

I had this issue for quite sometime with a SharePoint Online project I was working on. I use Design Manager to create a new layout page and add snippets to the html layout. I would create webpart zones, add my webpart snippets on to the html, save and publish but the resulting aspx page wouldn't have any of my webparts in it. I checked it from SPD 2013 and everything looks perfect, I can see all the code snippets in there. It was really strange and I had no clues. I had some content search webparts in the layout and one content editor webpart with a link to a text file with some css in it. After countless hours on the internet I read about few other people having similar issues when they had custom css on the layout pages to hide quick launch. I tried removing my content editor webpart and everything seems to be normal. A new page created using the layout had all the webparts in it and even the layout preview was displaying fine. I needed this css somewhere on the page and ...