5

I remember seeing somewhere that you can include an external JavaScript file in a SharePoint feature if it is in the _layouts folder. For the life of me I can't remember how to do this!

5 Answers 5

2
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
      ScriptSrc="JSDemo/jquery-1.4.2.js"
      Location="ScriptLink"
      Sequence="100"
      >
  </CustomAction>
</Elements>

Where JSDemo/jquery-1.4.2.js is in the /_layouts/ folder. /_layouts/ is prefixed automatically

1
1

You can deploy a javascript file from a Feature by placing it in a Module element, so that it is copied to the layouts folder, or the Style Library or any other library in the site.

Here's a tutorial on MSDN how to do it: How to provision a file. Here's another question on StackOverflow which asked something similar, but with XSL files. Just change the XSL files for your javascript files for the same effect (this is for 2007, but the resultant Elements XML file is identical).

2
  • "so that it is copied to the layouts folder" - Module element files can't be provisioned into the _layouts folder. Feature activation is done without any errors, but 404 not found error is returned, when you enter this address in the browser. If I then change "_layouts" to, for example, "files", and redeploy - file is now loaded fine. Commented May 19, 2011 at 10:19
  • Good point, missed that, to deploy to _layouts you need the TemplateFile element in the Solution manifest.xml Commented May 19, 2011 at 10:57
1

It depends on the scope you want.

If you want your script file to be scoped to the farm, deploy it to a folder under the _layouts folder (the _layouts folder itself should be reserved for files from Microsoft). You do this using a mapped folder in Visual Studio 2010 (http://msdn.microsoft.com/en-us/library/ee231521.aspx)

If you want your script file to be scoped to the site collection only, you can deploy it to the style library (as suggested by Etienne) but you need to make sure to secure it so end-users don't edit/delete it. Another option is to use a module (as suggested by James) to deploy the file to the virtual file system.

0

As James Love commented, you can only deploy to _layouts by editing the manifest.xml and hand rolling your DDF file. I've found STSDEV useful for helping me with this in MOSS 2007 but I don't know if it plays nicely with SPT 2010.

3
  • You don't have to hand-roll your manifest with WSPBuilder - since I started using WSPBuilder, I've never written one! As for SP2010 - Visual Studio's SharePoint Project's 'Mapped Folders' is all you need. It's very simple. Commented May 25, 2011 at 10:48
  • WSP Builder can't add items into _layouts. Commented May 26, 2011 at 0:12
  • It absolutely can. I've been using it to do that for years. It can deploy into, well, anywhere I've tried under 12 hive. Commented May 26, 2011 at 8:42
-1

You can place it here if you want: http://SITENAME/Style%20Library/Forms/AllItems.aspx

or

If you want to have some JavaScript to run on your page simple add a Content Editor Web Part on your page and place your JavaScript in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.