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
<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
-
1Giving credit where due: weblogs.asp.net/jan/archive/2010/03/01/…Anthony Graglia– Anthony Graglia2012-02-16 11:29:10 +00:00Commented Feb 16, 2012 at 11:29
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).
-
"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– Andrey Markeev2011-05-19 10:19:21 +00:00Commented May 19, 2011 at 10:19
-
Good point, missed that, to deploy to _layouts you need the TemplateFile element in the Solution manifest.xmlJames Love– James Love2011-05-19 10:57:02 +00:00Commented May 19, 2011 at 10:57
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.
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.
-
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– Andy Burns2011-05-25 10:48:56 +00:00Commented May 25, 2011 at 10:48
-
WSP Builder can't add items into _layouts.Mike H– Mike H2011-05-26 00:12:48 +00:00Commented 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.Andy Burns– Andy Burns2011-05-26 08:42:32 +00:00Commented May 26, 2011 at 8:42
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.