Skip to content

Instantly share code, notes, and snippets.

@sparsee
Last active March 19, 2018 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sparsee/bbab4ff54695add4e91dfab7dc8a6a9e to your computer and use it in GitHub Desktop.
Save sparsee/bbab4ff54695add4e91dfab7dc8a6a9e to your computer and use it in GitHub Desktop.
Set SharePoint team site's master page to inherit branding from the root node site collection master page
<a href="javascript:void(0)" onclick="TSUpdateSitesMaster()" style ="display:none" id="linkTSUpdateMaster" >Hello, Please Click Here to Update this site's Master Page</a>
<label id="lblUpdatedMaster" style ="display:none; color:green">This site's Master Page has been updated. Please refresh this page</label>
<label id="lblDeleteWebPart" style ="display:none; color:green">This site is inheriting the Master Page. Please remove this web part</label>
<label id="lblError" style ="color:red; display:none"></label>
<script type="text/javascript">
var m_scMasterUrl = "";
/*****************************
DCSharePointChick
Note: This code checks if master page of current site is the same as the top level root node of the SC master page. If not a match, it will reset the master page
This script should be added to your master page gallery branding folder. Add this script to a content editor web part with the link to the script in the branding folder.
Recommend that this web part be saved in a team site template, then hiding the original team site template from the site collection settings. Alternatively, you can save the web part to your web part gallery and make the web part available at will
******************************/
TSGetRootWebMasterUrl = function () {
// can check for current site master page here /_layouts/ChangeSiteMasterPage.aspx
//Get and load a reference to the root web of the current site collection.
var currCtx = new SP.ClientContext.get_current();
var site = currCtx.get_site();
this.rootWeb = site.get_rootWeb();
currCtx.load(this.rootWeb);
this.curWeb = currCtx.get_web();
currCtx.load(this.curWeb);
currCtx.executeQueryAsync(
function ()
{
m_scMasterUrl = rootWeb.get_masterUrl();
var currWebMasterUrl = curWeb.get_masterUrl();
var currWebCustomMasterUrl = curWeb.get_customMasterUrl();
// console.log( curWeb )
//alert( m_scMasterUrl )
if( m_scMasterUrl != currWebMasterUrl || m_scMasterUrl != currWebCustomMasterUrl)
{
document.getElementById("linkTSUpdateMaster").style.display = "inherit";
TSUpdateSitesMaster();
}
else
{
document.getElementById("lblDeleteWebPart").style.display = "inherit";
}
},
function (sender, args)
{
document.getElementById("lblError").innerHTML = args.get_message() + '\n' + args.get_stackTrace();
});
};
TSSUpdateSitesMaster = function()
{
var currCtx = new SP.ClientContext.get_current();
var curWeb = currCtx.get_web();
currCtx.load(curWeb);
curWeb.set_customMasterUrl( m_scMasterUrl);
curWeb.set_masterUrl( m_scMasterUrl);
curWeb.update();
currCtx.executeQueryAsync(
function ()
{
document.getElementById("linkTSUpdateMaster").style.display = "none";
document.getElementById("lblUpdatedMaster").style.display = "inherit";
//The below code will actually delete the web part. Remove the return statement when this code has been added to a site template (instructions above)
return;
/*Delete the webpart that has the code registered and refresh the page */
var pageName = _spPageContextInfo.serverRequestPath;
var ctx = new SP.ClientContext();
var pageFile = ctx.get_web().getFileByServerRelativeUrl(pageName);
var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var webPartDefs = webPartManager.get_webParts();
ctx.load(webPartDefs,'Include(WebPart)');
ctx.executeQueryAsync(
function ()
{
for(var i = 0;i < webPartDefs.get_count();i++)
{
var webPartDef = webPartDefs.getItemAtIndex(i);
var webPart = webPartDef.get_webPart();
if( webPart.get_title() == "Team Site Branding" )
{
webPartDef.deleteWebPart();
ctx.executeQueryAsync(
function ()
{
location.reload(true);
},
function (sender, args)
{
console.log(args.get_message());
});
}
}
},
function(sender,args){
console.log(args.get_message());
});
},
function (sender, args)
{
document.getElementById("lblError").innerHTML = args.get_message() + '\n' + args.get_stackTrace();
});
};
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
TSGetRootWebMasterUrl();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment