Thursday, April 11, 2019

SharePoint Team site Branding and Master Page Inheritance

It's been over a year since I posted about this topic, but I wanted to share how I'm now currently branding my SharePoint team sites (those that don't have publishing features turned on). This approach will work with 2013/2016 and possibly SharePoint online.

Side note: There's a lot of different approaches to do this. In the past you could use feature stapling. However, the new recommended approach is to use add-ins. My work environment is not currently set up to use SharePoint add-ins. My role is a developer and not an administrator, so I can't even set this up because there is a clear separation of duties. Also, we're in the very slow process of migrating to a new version, so any discussion of having this set up now is out of the question.

Instead of my previous approach, I decided I could script this out in a custom action. What's a custom action? You may recall custom actions in SharePoint designer. You could add these to the ribbon. You could also add custom actions with features and .NET code. That's exactly right! However, I'm referring to using a custom action with a JavaScript file that is referenced in the site or entire site collection and is basically hidden in the site. There's no UI to add the custom action. It can however be added by Powershell

SharePoint PNP powershell commands makes it easy to add these to the site collection.

The idea here is to automatically set the master page as soon as a non-publishing site is created, or to have the option to inherit the look and feel from the site itself. There's no clear way to do either from the UI. With the JavaScript registered as a custom action on the site collection, any sub-site will run the JavaScript. The JavaScript will check to see if the site has just been created. If so, it will automatically update it with it's parent's master page, which you will see instantly change on the screen. Otherwise, it will exit out. If the team site was created a while ago but doesn't have the same branding as it's parent, users can go to Change the Look of the site from the gear icon. There, you will see a new red banner that allows you to inherit the branding of the parent.








In my example, I grab the parent master page because I have a publishing site collection with team sites. You can change this example to explicitly state which master page you want to use.


The PNP commands are simple:


  1. Get the file from here https://gist.github.com/sparsee/0bb2218a2b42ba679d12b96ce7d645ae
  2. From the server, open the Powershell Prompt
  3. Connect to the site collection
    1. You may get prompted for credentials
  4. Run the command to upload the javascript file from a folder on the server into the site collection's branding folder
  5. Run the command to register that javascript file as a custom action using the Add-PnPJavaScriptLink pnp command
  6. That's it! Create a team site in your site collection and voila!

$spSiteCollectionUrl = "http://myweb/sites/testsitecollection" 


Write-Host "Connecting to Site..." -foregroundcolor black -backgroundcolor yellow 
Connect-PnPOnline -Url $spSiteCollectionUrl 
Write-Host "Connection Established successfully" -foregroundcolor black -backgroundcolor green 

$Web = Get-PnPWeb 



 UploadFile "\newbranding\scripts\CustomActions\CustomActionTeamSiteBranding.js" "_catalogs/masterpage/NewBranding/scripts/CustomActions"

$customActionJsLink = -join($Web.Url, "/_catalogs/masterpage/NewBranding/scripts/CustomActions/CustomActionTeamSiteBranding.js");
    Write-Host 'Adding custom action link... ' $customActionJsLink -foregroundcolor black -backgroundcolor yellow
    Add-PnPJavaScriptLink -Name CustomActionTeamSiteBranding -Url $customActionJsLink -Scope Site
    Write-Host "Completed adding custom action" -foregroundcolor black -backgroundcolor green

Note:
This strategy is part of the overall site provisioning. When I create new site collections, I have a much more involved script that adds site collection admins directly, turns on publishing features, adds all of the branding files (master pages, page layouts, css, etc), creates home pages with default web parts, and adds the custom action javascript files. I hope to share that script on this site very soon!