2

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?

2
  • 1
    Sandboxed or Farm Solution?
    – James Love
    Commented Apr 6, 2011 at 17:18
  • This is a Farm solution Commented Apr 11, 2011 at 2:22

4 Answers 4

7

Yes - see @markt's answer

SO - How to call an external javascript file from a webpart

...you 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)

Layouts

  • 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

3
  • 1
    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
    Commented Apr 6, 2011 at 17:21
  • Good point, No - editing now.
    – Ryan
    Commented Apr 6, 2011 at 20:19
  • 1
    You guys rock!!! Commented Apr 6, 2011 at 21:53
1

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.

1
  • The issues outlined here also apply to any other JavaScript library, like ProtoType or MooTools.
    – James Love
    Commented Apr 8, 2011 at 12:40
0

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.

http://code.google.com/apis/libraries/devguide.html#jquery http://www.asp.net/ajaxlibrary/cdn.ashx#jQuery_Releases_on_the_CDN_0

They have SSL versions, too, I think.

 Tim
0

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)

http://spc3.codeplex.com/wikipage?title=ClientScript&referringTitle=Home

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.