I have seen This Post, It works for me but I want to add Javascript code to all SharePoint sites of particular site Collection. Is it possible add Javascript code to all the sites and sub-site of the particular site-collection?


I'm using SharePoint Online. To add Javascript code into Current Site, I have used UserCustomActions to add Javascript code in all the Pages of the current SharePoint site.

1 Answer 1


Note, MS has disabled UCA on the modern lists and libraries view, so the below code wont work there. But on classic pages, it will work and load the javascript file on all pages of subsites of a site collection.

Site site = context.Site; //gives you the current site collection

string jsLink = "path-to-jquery.js-file";  //change this as per your jquery file location

StringBuilder scripts = new StringBuilder(@"
    var headID = document.getElementsByTagName('head')[0]; 

    newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = '{0}';
    headID.appendChild(newScript);", jsLink);
string scriptBlock = scripts.ToString();

//get all UCA belonging to current site collection
var existingActions = site.UserCustomActions;
var actions = existingActions.ToArray();

//delete the UCA if it already exists
foreach (var action in actions)
    if (action.Description == "scenario1" &&  action.Location == "ScriptLink")

var newAction = existingActions.Add();
newAction.Description = "scenario1";
newAction.Location = "ScriptLink";

newAction.ScriptBlock = scriptBlock;
context.Load(site, s => s.UserCustomActions);

Modified code from here - Customize your SharePoint site UI by using JavaScript

  • 1
    Use the Chrome Extension: SPEditor (SharePoint Editor) to manage all this with a GUI Commented May 23, 2017 at 6:54
  • Is it possible to somehow UCA work on the modern lists and libraries view? Commented May 23, 2017 at 7:49
  • No, MS has disabled it on modern view. If you want your UCA to run , then disable Modern view from the tenant and ensure that lists and libraries are in classic mode. UCA wont work on modern lists and libraries. Upvote on the user voice link - sharepoint.uservoice.com/forums/329214-sites-and-collaboration/… Commented May 23, 2017 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.