SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Basically I would like to create a WebPart that uses jQuery but I don't want to have to add jQuery to my masterpages (or anywhere else) manually. I would like to have one WSP that I could email to someone, have them install only that, and having a working WebPart that uses jQuery.

Is this possible?

share|improve this question
Sandboxed or Farm Solution? – James Love Apr 6 '11 at 17:18
This is a Farm solution – Abe Miessler Apr 11 '11 at 2:22
up vote 7 down vote accepted

Yes - see @markt's answer

SO - How to call an external javascript file from a webpart should be deploying your scripts to layouts, along with images, stylesheets, etc. that are not intended to be customized by your users.

You can map the folder "Layouts" to your project in VS 2010. Then add subfolders to reflect your project name, etc. (Right click on project -> Add-> SharePoint "layouts" Mapped Folder)


  • ProjectName
    • Scripts
      • jquery-1.5.min.js

Then, when you deploy your solution, the scripts will be copied to the proper location..

In your webpart, you can reference your scripts like:

In code:

ScriptLink.Register(this.Page, "ProjectName/Scripts/jquery-1.5.min.js", false);false);

EDIT - As James points out you can't used the Layouts mapped folder or ScriptLink.Register() if you are writing a sandbox solution.

In this case you should do something very similar, but using an SiteAssets (module) folder, Lightening Tools have a good post about this

share|improve this answer
Was just about the hit Submit when this came through :) Spot on, the focus is the ScriptLink.Register() call - this will let you include the library without modifying the masterpage. Ryan: Will this work in the Sandbox (specifically: can you deploy to mapped folders in the Sandbox)? – James Love Apr 6 '11 at 17:21
Good point, No - editing now. – Ryan Apr 6 '11 at 20:19
You guys rock!!! – Abe Miessler Apr 6 '11 at 21:53

Thing with jQuery and I appreciate your pain is that ANY one with ANY webpart or masterpage or doc library can be adding it in.

Your have to hope that your code will work with multiple jQuery libraries and you should test it with all of them, even the great SPServices had to make a slight change to be compatible thru the versions.

Now that said how do you load jQuery in your webpart.

Well the manner in which I did it was to copy the way Jaap Vossers did it in his Dev Dash Visualiser.

This uses a ScriptLink custom action to load a custom JS Script and the LABJS library. That script uses LABJS to determine if jQuery is already loaded and if so it will dynamically load it if it is needed.

So the next thing is what if LABJS is loaded, well if your worried about this on the LABJS site is some code that will dyno load that too.

So where to host the jQuery and LABJS libraries, any where you like CDN's are good, so is _layouts, but also a folder in a site collection (not necessarily a doc lib), just remember to namespace it in _layouts to your project else there may be collisions, if you copy and rename a lib to _layouts/jquery.min.js and yours is v1.3 and other have done the same with 1.5 eeek! So namespace it or specifically version it.

Until MS or whoever can come up with a centralised way to manage jQuery library inclusion in the project, Dyno loading the lib in your custom part is the way to go, unless of course its your SharePoint installation and you have control over all of it.

Jan Teilens blog also has some suggestions on how to do this it is worth examining them all.

share|improve this answer
The issues outlined here also apply to any other JavaScript library, like ProtoType or MooTools. – James Love Apr 8 '11 at 12:40

For jQuery, use one of the CDNs. Any users that already have these .js files cached from any other website they go to won't have to download it from your site, speeding up their initial page load.

They have SSL versions, too, I think.

share|improve this answer

I built a sandbox wsp a couple of months ago to add jquery to sites. You just need to activate the feature depending on where you want to pull jquery from (cdn or sharepoint library)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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