I have a list of urls pointing to images, which are originated in various subsites - sometimes burried in nested folder structures. Now, my question is: Is there any way I can derive the url of the containing subsite from such image url within the code of a display template?

I know it's not possible by pure string operations, since the folder structures have no clear pattern, but I hoped I somehow could get the context of the subsite and work from there. Something like this:

var ctx = SP.ClientContext(imageUrl);
var subsiteUrl = ctx.get_web().get_url();

Unfortunately, the context I receive from the imageUrl is invalid. Any ideas? Thx!


It's normal behavior to receive invalid context from image URL where you should provide absolute Site URL.

So You will need to split the URL first to get the Site URL as mentioned at How to split URL to get URL path in javascript

Alternatively, Instead of splitting image URL to get site URL you can use SP.ClientContext.get_current() as the following:

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(loadSiteURL, "sp.js"); });
function loadSiteURL() {
var ctx = new SP.ClientContext.get_current();
this.site = ctx.get_site();
this.web = ctx.get_web();
ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));

function onSuccess(sender, args) {
funtion onFail(sender, args) {
  • thank you for your quick response! However, this is actually not what I am lokking for. As I mentioned, the images are stored in folders of different depths like "mysitecollection.com/subsite1/folder1/folder2/imageA.jpg" and "mysitecollection.com/subsite2/subsite2b/folder1/imageB.jpg". So I cannot use string splitting in order to receive the urls "mysitecollection.com/subsite1/" and "mysitecollection.com/subsite2/subsite2b". It's just not a clear pattern to work with.
    – Thassilo
    Jun 17 '17 at 21:50
  • and your second suggestions requires that I already know the parent subsite of the images. But that's exactly what I am lokking for. And in the general case it is NOT the one I am currently in
    – Thassilo
    Jun 17 '17 at 21:56
  • Bro, why you concentrate on image URL ?! where will you add your code? so SP.ClientContext.get_current will solve your issue I'm confused why you restricted with image URL! Jun 20 '17 at 23:35
  • It does not matter whether the URL refers to an image or any other resource. The problem is, that with SP.ClientContext.get_current(), ctx.get_site() and ctx.get_web() I only get the CURRENT site/page. But I am interested in the site where the image (or other resource) lays in. And the only information I have is the absolute url of this image/resource.
    – Thassilo
    Jul 15 '17 at 10:40

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .