Skip to content

Instantly share code, notes, and snippets.

@sparsee
Last active April 15, 2019 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sparsee/0bb2218a2b42ba679d12b96ce7d645ae to your computer and use it in GitHub Desktop.
Save sparsee/0bb2218a2b42ba679d12b96ce7d645ae to your computer and use it in GitHub Desktop.
SharePoint Team Site Branding using Custom Action
/**********************************************
This script is a Custom Action, that is activated through powershell on each site collection
after it has been provisioned. The custom action is not attached through the master page.
More info can be found on msdn.
This custom action is to make sure that when a sub-site is created, it takes on the branding.
Normally, team sites do not inherit the branding.
More information found here: https://dcsharepointchick.blogspot.com/2019/04/sharepoint-team-site-branding-and.html
************************************************/
var DCSharepointChick = window.DCSharepointChick || {};
DCSharepointChick.CustomAction = DCSharepointChick.CustomAction || {};
function InitDCSharepointChickCustomAction()
{
DCSharepointChick.CustomAction.CA = new DCSharepointChick.CustomAction.NewCustomAction();
DCSharepointChick.CustomAction.Init();
}
DCSharepointChick.CustomAction.Init = function()
{
}
DCSharepointChick.CustomAction.NewCustomAction = function()
{
//publishing feature
//var mPublishingfeatureGuid = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb"
var m_keyRedirectedFromNewSubSite = "DCSharepointChick.Collab.NewSubsite";
if( window.location.href.toLowerCase().indexOf( "/_layouts/15/newsbweb.aspx" ) > -1 )
{
sessionStorage[ m_keyRedirectedFromNewSubSite ] = true;
return;
}
if( !IsSiteToCheck() )
{
return;
}
var m_bnewSiteCreated = false;
if( sessionStorage[ m_keyRedirectedFromNewSubSite ] != null && sessionStorage[ m_keyRedirectedFromNewSubSite ] == "true")
{
m_bnewSiteCreated = true;
sessionStorage[ m_keyRedirectedFromNewSubSite ] = null;
}
//checks that the current location is not the site contents page. If it is, return.
//Scenario this may hit: If you're in the site contents of a team site that has it's own branding/style,
//then, you go to create a new sub-site, but then you cancel out of the page. In that case, we don't want
//to update the branding of the current site
if( window.location.href.indexOf("/_layouts/15/viewlsts.aspx") > -1 )
{
return;
}
var curCACtx = new SP.ClientContext.get_current();
curCurWeb = curCACtx.get_web();
curCACtx.load( curCurWeb );
//var themeInfo = curCurWeb.get_themeInfo();
//curCACtx.load(themeInfo);
if( m_bnewSiteCreated )
{
m_fnCheckUserHasManageWebPermissions (function( userHasManagePerms )
{
if( userHasManagePerms )
{
//checks if the previous page that we have been redirected from was the site creation page. If it was, then a new site is being created and continue
//if( (document.referrer && document.referrer.toLowerCase().indexOf( "/_layouts/15/newsbweb.aspx" ) > -1 ) || prevPage.indexOf( "/_layouts/15/newsbweb.aspx" ) > -1 )
m_fnCheckAndUpdateMasterUrl( function ( doneChecking )
{
if( doneChecking )
{
//alert("donechecking")
}
});
}
});
}
else if( window.location.href.indexOf("_layouts/15/designgallery.aspx") > -1 )
{
var htmlLink = "<div style='padding-top:10px;padding-left:30px'>If the branding is inconsistent, <a href='javascript:void(0)' onclick='DCSharepointChick.CustomAction.CA.CheckAndUpdateMasterUrlOnDesignGallery()'>click here</a> to inherit the branding from the parent</div>";
statusId = SP.UI.Status.addStatus("Attention: It is not recommended to change the look and feel of this site to be inconsistent with the Custom Branding" + htmlLink);
SP.UI.Status.setStatusPriColor(statusId, 'red');
}
function m_fnCheckAndUpdateMasterUrl( OnComplete)
{
//offering flexibility here
//if after the site is provisioned and the end user really wants a special theme, let them have it and dont update the master
//here we can only pull the theme background image, not the title of the theme;
//so if the theme has a special background image, we'll let them keep it, otherwise, we'll reset it if needed
//This should all go away when we do real site provisioning, because the code will execute only once when the site is created
//if( themeInfo.get_themeBackgroundImageUri() != null )
//{
// return;
//}
var curCurParentWeb = null;
var curParentInfo = null;
curParentInfo = curCurWeb.get_parentWeb();
curCACtx.load( curParentInfo );
function OnSuccess(sender, args)
{
curCurParentWeb = curCACtx.get_site().openWebById( curParentInfo.get_id() );
curCACtx.load( curCurParentWeb );
function OnSuccess1(sender, args)
{
var parentWebMasterPageUrl = curCurParentWeb.get_masterUrl();
var parentWebCustomMasterPageUrl = curCurParentWeb.get_customMasterUrl();
var currWebMasterPageUrl = curCurWeb.get_masterUrl();
var currWebCustomMasterPageUrl = curCurWeb.get_customMasterUrl();
var currCtx = new SP.ClientContext.get_current();
var curWeb = currCtx.get_web();
currCtx.load(curWeb);
var bDoUpdate = false;
if( parentWebMasterPageUrl != "" && parentWebMasterPageUrl != currWebMasterPageUrl )
{
curWeb.set_masterUrl( parentWebMasterPageUrl );
curWeb.update();
bDoUpdate = true;
}
if( parentWebCustomMasterPageUrl != "" && parentWebMasterPageUrl != currWebCustomMasterPageUrl )
{
curWeb.set_customMasterUrl( parentWebMasterPageUrl );
curWeb.update();
bDoUpdate = true;
}
if( curWeb.get_themedCssFolderUrl() != null)
{
curWeb.set_themedCssFolderUrl( null );
curWeb.update();
}
if( bDoUpdate )
{
currCtx.executeQueryAsync
(
function ()
{
if( window.location.href.indexOf("/_layouts/15/designgallery.aspx") >=0 )
{
window.location.href = curCurWeb.get_url();
}
else
location.reload( true );
},
function (sender, args)
{
this.OnComplete( false );
}
);
}
else
{
if( window.location.href.indexOf("/_layouts/15/designgallery.aspx") >=0 )
{
window.location.href = curCurWeb.get_url();
}
this.OnComplete( true );
}
}
function OnFailure1(sender, args)
{
//alert('Fail: ' + args.get_message() + '\n' + args.get_stackTrace());
this.OnComplete( false );
}
var data = { OnComplete: OnComplete }
curCACtx.executeQueryAsync(Function.createDelegate(data, OnSuccess1), Function.createDelegate(data, OnFailure1));
}
function OnFailure(sender, args)
{
OnComplete( false );
}
var data = { OnComplete: this.OnComplete}
curCACtx.executeQueryAsync(Function.createDelegate(data, OnSuccess), Function.createDelegate(data, OnFailure));
}
function m_fnCheckAndUpdateMasterUrlOnDesignGallery()
{
if( confirm("Are you sure you would like to inherit the branding?") )
{
var doneChecking = false;
m_fnCheckAndUpdateMasterUrl( function ( doneChecking )
{
if( doneChecking )
{
//alert("donechecking")
}
});
}
}
function m_fnCheckUserHasManageWebPermissions( OnComplete )
{
curCACurrentUser = curCurWeb.get_currentUser();
curCACtx.load( curCACurrentUser );
curCACtx.load( curCurWeb, 'EffectiveBasePermissions');
function OnSuccess(sender, args)
{
//check if User Has Edit Permissions
if ( curCurWeb.get_effectiveBasePermissions().has( SP.PermissionKind.manageWeb ) )
OnComplete( true );
else
OnComplete( false );
}
function OnFailure(sender, args)
{
OnComplete( false );
}
curCACtx.executeQueryAsync(OnSuccess, OnFailure);
}
function m_fnGetCurWebUrl()
{
var curWebUrl = window.location.href;
var indx = curWebUrl.indexOf( "/_layouts/15/designgallery.aspx");
curWebUrl = curWebUrl.substr(0, indx);
}
function IsSiteToCheck()
{
//If site template is team site, project site, community site or blog, then we will check it
if( _spPageContextInfo.webTemplate == "1" || _spPageContextInfo.webTemplate == "6115" || _spPageContextInfo.webTemplate == "62" || _spPageContextInfo.webTemplate == "9" )
{
return true;
}
return false;
}
var publicMembers =
{
CheckAndUpdateMasterUrlOnDesignGallery: m_fnCheckAndUpdateMasterUrlOnDesignGallery
}
return publicMembers;
}
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function()
{
ExecuteOrDelayUntilScriptLoaded(function () { InitDCSharepointChickCustomAction(); }, "core.js");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment