4

I'd like to add jQuery and SPServices.

I added the following code:

<SharePoint:ScriptLink runat="server" Name="/Style%20Library/Scripts/jquery-1.7.2.min.js" Localizable="false"></SharePoint:ScriptLink>
<SharePoint:ScriptLink runat="server" Name="/Style%20Library/Scripts/jquery.SPServices-0.7.1a.js" Localizable="false"></SharePoint:ScriptLink>

to the v4.master at the bottom of the tag. I added both files to the root site's Style Library.

Everything was working fine on some sub sites. However, sometimes I got the following error:

Cannot make a cache safe URL for "/style%20library/scripts/jquery-1.7.2.min.js", file not found. Please verify that the file exists under the layouts directory. 

Where should I be putting these javascript files?

1 Answer 1

11

If you can deploy to _layouts (i.e. not restricted to sandbox) and you anticipate needing these libraries across many site collections, I would deploy them to a folder within _layouts. Generally, it's good practice when deploying into _layouts to deploy to a subdirectory within _layouts, something like "_layouts/MyCompanyOrMyProduct" to avoid file naming conflicts. Deploying to _layouts will make longer term maintenance/upgrades easier if these libraries are being used across site collections. If you needed localizable copies of these files, you'd deploy them to the appropriate LCID folder under _layouts (for example _layouts/1033/MyCompanyOrProduct), but this doesn't sound like it will be needed in this case.

In this case, you could bypass the script link control all together, but it can add some value (like versioning), so it's probably worthwhile using <ScriptLink> to reference it from within the _layouts folder. You reference them in this way (without specifying _layouts path - the script link assumes _layouts by default):

<SharePoint:ScriptLink runat="server" Name="/MyCompanyOrProduct/jquery-1.7.2.min.js" Localizable="false"></SharePoint:ScriptLink>
<SharePoint:ScriptLink runat="server" Name="/MyCompanyOrProduct/jquery.SPServices-0.7.1a.js" Localizable="false"></SharePoint:ScriptLink>

If you make "localizable" versions of your scripts, and you use the for getting the scripts onto the page, be sure to set the Localizable="true" for your .

If you are constrained to the Sandbox, the Style Library is the correct location.

If you are going to keep them in the Style Library can try referencing the scripts using the ~sitecollection replacement token (generally Style Library is only provisioned at the root site within a site collection). You might be getting the errors described if you are in a subsite and the scripts are deployed to the root site of the site collection. For example:

<SharePoint:ScriptLink runat="server" Name="~sitecollection/Style%20Library/Scripts/jquery-1.7.2.min.js" Localizable="false"></SharePoint:ScriptLink>
<SharePoint:ScriptLink runat="server" Name="~sitecollection/Style%20Library/Scripts/jquery.SPServices-0.7.1a.js" Localizable="false"></SharePoint:ScriptLink>

I normally try these solutions out before posting, but don't have a VM handy at the moment.

You can also check out some other answers on this same topic here. Note: this question could be flagged as duplicate and closed. See: Where do you deploy scripts that are loaded in a masterpage? and this one from a few years ago, but still relevant: CSS Files in _Layouts or Document Library?

1
  • Is it faster to have the scripts in the layouts folder than a style library? It seems like these files get loaded from disk cache whereas files from libraries do not and take longer to load.
    – Batman
    Apr 2, 2017 at 15:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.