Add Custom Banner to SharePoint Site without changing master page


Few days ago, I got a question if it is possible to add a banner message to a SharePoint Online or On-premises site without customizing the master page.  They were looking for a way to enable and disable this using the Feature framework so I had to write a feature event receiver to upload the customized master page.  The customer was happy but I was not.

I did some research and ended up finding something that makes it lot easier to do such things.  You can do it using Code using the event receiver but I wanted to make sure you do not deploy any server side solution.  The script below using CSOM to get things done.  All you need is the jquery file and the custom js file uploaded to a CDN or SharePoint assets library.  If you are running off the server then you need to deploy SharePoint client side assemblies from Microsoft download center.

Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”

function Add-SPBanner($SiteUrl, $Credentials)
{
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName,$Credentials.Password)
$site = $context.Web
$context.Load($site)
$context.ExecuteQuery()

$UserCustomActions = $site.UserCustomActions
$context.Load($UserCustomActions)
$context.ExecuteQuery()

$newAction = $UserCustomActions.Add()
$newAction.Location = “ScriptLink”
$newAction.scriptSrc = “~SiteCollection/Style Library/jquery.min.js”
$newAction.Sequence = 30000
$newAction.Title= “BannerJquery”
$newAction.Update()
$context.ExecuteQuery()

#add custom js injection action
$customJSAction = $UserCustomActions.Add()
$customJSAction.Location = “ScriptLink”
#reference to JS file
$customJSAction.ScriptSrc = “~SiteCollection/Style Library/test.js”
#load it last
$customJSAction.Title= “BannerJS”
$customJSAction.Sequence = 30001
#make the changes
$customJSAction.Update()
$context.ExecuteQuery()

Write-Host “Banner has been Added…” -ForegroundColor Green
}

function Remove-SPBanner($SiteUrl, $Credentials)
{
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName,$Credentials.Password)
$site = $context.Web
$context.Load($site)
$context.ExecuteQuery()

$UserCustomActions = $site.UserCustomActions
$context.Load($UserCustomActions)
$context.ExecuteQuery()

$UserCustomActions | ? Title -Like “Banner*” | Select Title, Sequence
if($UserCustomActions.Count -gt 0)
{
$CA = $UserCustomActions | ? Title -eq “BannerJquery”
$CA.DeleteObject()

$CA = $UserCustomActions | ? Title -eq “BannerJS”
$CA.DeleteObject()
$context.ExecuteQuery()
Write-Host “Banner has been Removed…” -ForegroundColor Green
}
}

$Creds = Get-Credential
$SiteUrl = “https://sharepointmvp.sharepoint.com”

Add-SPBanner -SiteUrl $SiteUrl -Credentials $Creds
Remove-SPBanner -SiteUrl $SiteUrl -Credentials $Creds

Code on Technet

https://gallery.technet.microsoft.com/Add-Custom-Banner-to-dbbf22d3