How do you include a JavaScript file from within a SharePoint WebPart?

We have a medium sized .js file that we include in our web framework that I am porting over to SharePoint. However, I'm not sure how to go about this or what the best practice is. This is for a framework solution that will be used by other client projects, so it's best for it to be self contained and deploy-able, rather than requiring manually deploying files to the webserver.

My current thinking to put the JavaScript into an embedded resource and then use the script manager to write out the file. Does this seem reasonable? Or does anyone have any other recommendations?


Embeded resource is the best way and you don't need to use the ScriptManager to render it out (as AJAX is not configured OoB on SharePoint), you can just render it as any other client script resource (through the ClientScriptManager).

Best idea is the have an if ContainsScriptManager else UsClientScriptManager style. That way you get the best of both worlds

You could just toss it into a doc library.

If you are packaging your web part into a sharepoint solution, you could include it as a Module (VSEWSS item). Your manifest file would get something like:

  <TemplateFile Location="LAYOUTS\somescript.js" />

Theres lots of info on how to do this on the web. I've liked the doc library option for images, css, and javascript because I don't have to rebuild and wait for for SP to JIT compile for 30 seconds each time I do a minor tweak to some style or script. I just edit these things in SPDesigner right out of the doc library.

Create resource mapped folder in your project and put the js file there and give the referene of the js file as _layout/jsfilename.js and this js file will be deployed with your project where ever you will deploy it.

Need Your Help

Expand and collapse issues


I'm a rookie designer having a few troubles with this page: