Add JavaScript Libraries to SharePoint 2010 with ScriptSrc

Using ScriptSrc attribute in SharePoint 2010, add JavaScript libraries without delegate control. Create a custom action & add a relative reference.

I saw another blog post today where someone was showing how to use a delegate control to add the jQuery library to every page on a site. While that does work, there is a newer and much more elegant solution that doesn’t require nearly the same amount of work: the ScriptSrc attribute introduced in SharePoint 2010.

All you have to do is create a new custom action in a Feature and add a relative reference to the jQuery, or any JavaScript library for that matter. The library reference will be added to every page for the scope of the Feature so using this technique you could even add it to every single page in the farm. Here’s what it looks like…

I’ve only found two downsides to this approach. The first is that the jQuery library isn’t being pulled from a CDN like Google or Microsoft’s hosted options. I prefer to pull from these as it’s likely the user has already pulled from them before and it’s cached on their machine. The other downside is that I’ve seen this approach break the Asset Picker in SharePoint 2010 Publishing [WCM] sites.

One parting thought: I don’t think it’s a good idea to globally add a script to every page UNLESS you need it on every page. Else you’re simply adding page weight for no good reason.

Andrew Connell
Developer & Chief Course Artisan, Voitanos LLC. | Microsoft MVP
Written by Andrew Connell

Andrew Connell is a web & cloud developer with a focus on Microsoft Azure & Microsoft 365. He’s received Microsoft’s MVP award every year since 2005 and has helped thousands of developers through the various courses he’s authored & taught. Andrew’s the founder of Voitanos and is dedicated to helping you be the best Microsoft 365 web & cloud developer. He lives with his wife & two kids in Florida.

Share & Comment