Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
add comment

5 Answers 5

up vote 3 down vote accepted
<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

share|improve this answer
    
Giving credit where due: weblogs.asp.net/jan/archive/2010/03/01/… –  trgraglia Feb 16 '12 at 11:29
add comment

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

share|improve this answer
    
"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. –  Andrey Markeev May 19 '11 at 10:19
    
Good point, missed that, to deploy to _layouts you need the TemplateFile element in the Solution manifest.xml –  James Love May 19 '11 at 10:57
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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. –  Andy Burns May 25 '11 at 10:48
    
WSP Builder can't add items into _layouts. –  Mike H May 26 '11 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. –  Andy Burns May 26 '11 at 8:42
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

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.