This is the first of a few blog posts I wanted to cover in my 2015 Christmas break on the future of branding.
I'll probably devote the next article to compare the various different ways to brand your SharePoint site - masterpage? CustomCss? On-premises or Online? Would the technique work in SharePoint 2010? What about display templates or JSLink?
But I want to march forward and cover what I think is the safest way to brand your SharePoint right now:
Inject JavaScript to any Site or Site Collection via a User Custom Action's ScriptLink property.
This is a great technique because it lets you:
- Add JavaScript anywhere - scoped to Site Collection or Site.
- Add CSS via JavaScript
- You can add more than 1 CSS file
- Order them the way you want via Sequence
- You can combine this to load your initial JavaScript file which can be a RequireJS setup and then hand off the controls to RequireJS config
- Does not modify MasterPage
- Works in SharePoint 2010, 2013, 2016 and SharePoint Online
- Only need Site Collection permissions to set up - you don't need to have a Farm Solution or Add-In Model. The permission is only required to set up the ScriptLink.
- The object model provides a way for an administrator to check all the User Custom Actions attached to any site/site collection, so there's a level of oversight available if you want to check if your customizations are ready for migration.
There are various ways to attach a script via User Custom Actions.
- Remote Provisioning (Pattern and Practice) uses it via C# CSOM
- PowerShell remote provisioning
- Farm Solution can invoke the API
- Sandbox Solution can invoke the Client Side Object Model API (*with permission)
- Add-In can invoke the CSOM API as well (with Site Collection - Full Control permission)
The unfortunate part is, there's no UI for a power user to add or view ScriptLinks directly. You need to spin up SharePoint Manager or read it via PowerShell.
And that brings me to today's post.
I build a simple config page in JavaScript.
Then I did a load of work to make sure everything runs from One Page.