Skip to main content
3 of 3
replaced http://stackoverflow.com/ with https://stackoverflow.com/

Yes - see @markt's answer

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

...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][2]

[1]: https://stackoverflow.com/questions/5068663/how-to-call-an-external-javascript-file-from-a-webpartfalse); [2]: http://lightningtools.com/blog/archive/2009/12/06/add-javascript-files-once-to-a-page---sharepoint-sandbox.aspx

Ryan
  • 5.8k
  • 8
  • 29
  • 54