0

I'm trying to reference a JS file in (multiple) site-collections by using a Custom Action with Location=ScriptLink as described by sadomovalex. Referencing the file in the site-collection where the file is located (Style Library) only works when the ScriptSrc is given as

~siteCollection/Style Library/myLib.js

When I try using the server-relative or absolute path which I would need for other site-collections like

/Style Library/myLib.js

or

http://myserver.local/Style Library/myLib.js

every single page dies without a user visible error just displaying nothing and the ULS give me these errors

Unexpected  CannotMakeBrowserCacheSafeLayoutsUrl: 15/0/0//style library/mylib.js

or

Unexpected  CannotMakeBrowserCacheSafeLayoutsUrl: 15/0/0/http://myserver.local/style library/mylib.js

but no further hint, what I might be doing wrong.

Do you have any suggestions to get this working properly?

0

Based on the fact that the error ends with "LayoutsUrl" and the wrong URLs start with "15", my guess is that without the site collection URL token, SharePoint is assuming that the URL is to somewhere on the file system (i.e. in the /_layouts/15/ directory).

If you follow the link at the end of the blog post you mention, he wrote another blog post:

Add custom javascript file to all pages in Sharepoint Online site collection

And in that post, to quote the end of it:

In this script we at first enumerate all tenant site collections (lines 136-160). For each site collection we at first upload custom javascript file to Site Assets doclib (lines 40-78) and then add custom action with script reference on added javascript file (lines 80-110). As result our custom javascript file will be added to all pages in all site collections in Sharepoint Online web app.

So, he is uploading the script file to every site collection, and using the ~sitecollection URL token in every script link / custom action.

I'm not sure you can do what you want if you only upload it to one site collection. If you want the file to be in only one place, you may have to put it in the /_layouts/15 directory.

If you notice in the first blog post (the one you link to), he is putting the script file in the /_layouts/15/ directory.

  • Note that SPOnline allows any https url. So you could link to one file on a Microsoft O365 CDN or your own https server acting as a CDN (which is what we do, because we have full control over the JSfiles.. and Microsofts CDN has a too long delay in caching files) – Danny '365CSI' Engelman Jan 3 '17 at 21:05
  • @Dylan I wanted to get around uploading to _layouts or maintaining multiple files. Today I read a post which elaborated on the path resolution of the ScriptSrc-property. And it clearly said what I was trying should be possible. I've been digging for 15 minutes now but I can't find it anymore. At least linking to a file on a CDN should be possible, I guess (someserver.com/somefile.js) – ben Jan 3 '17 at 21:33
0

I added some details to the mentioned post's comments since Ben asked also there: "some time ago I also faced with similar strange issue: looks like Sharepoint somehow checks that file referenced in ScriptSrc really exists. If file not found, pages got broken. In example in this post I used js file from /_layouts subfolder, which as you probably know can be opened in context of any Sharepoint site, i.e. suppose that example.com is root site of site collection and example.com/test is one of its sub sites, then we will have:

http://example.com/_layouts/15/test/foo.js
http://example.com/test/_layouts/15/test/foo.js

both files will be opened.

But when you try to add js file into Sharepoint doclib, e.g. to Style library, it will be accessible only by absolute url example.com/Style library/foo.js. So if you use relative url and try to open page on sub site it will try to find it on example.com/test/Style library/foo.js and won't find it here. That's why in last case ~sitecollection is needed."

In addition to that in order to restore broken site (which is not loaded because of incorrect ScriptSrc) you may use similar PowerShell script, but instead of adding custom action call $a.Delete() in the loop which checks that such action is already added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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