1

Given an absolute URL to an item, like:

http://server/sites/mysite/myweb/Lists/MyList/DispForm.aspx?ID=1

or the absolute URL to the item's parent list, like:

http://server/sites/mysite/myweb/Lists/MyList/AllItems.aspx

how can I get the URL of the item's parent web, in JSOM?

(In this case, http://server/sites/mysite/myweb)

This is in the context of an Item Display Template for a Content Search web part. I know that there is a property you can retrieve from the ctx.CurrentItem called SPSiteURL, but that is either the URL of the site collection or the URL of the site that the Content Search webpart is on (they happen to be the same in my case, hence the ambiguity), and I want the URL of the sub-web that the item is on.

What's the best way to do this? I know I can resort to string manipulation (i.e. cut off the item's URL where "/Lists..." starts), but I'd rather do it a better way.

EDIT

Just to be clear, the Content Search web part where this code will be executing is in the root web of the site collection: http://server/sites/mysite

The item is in a list in a subsite: http://server/sites/mysite/myweb/Lists/MyList/AllItems.aspx

What I am looking for is the URL of the web where the list that the item comes from is located: http://server/sites/mysite/myweb

Which in this case is below where the code is executing in the site hierarchy.

2
  • you already tried use this code : SP.ClientContext.get_current().get_web().get_parentWeb() ?
    – b1c10
    Commented Jan 9, 2015 at 17:03
  • The Content Search web part I am using is in a site that is above where the item is coming from. I am in the site collection root web, and the item is in a sub web. Commented Jan 9, 2015 at 17:19

1 Answer 1

1

Try this

var URL = "http://site/site/file.aspx";

$.ajax({
    url: URL,
    type: "POST",
    contentType: "application/x-www-url-encoded",
    dataType: "json",
    headers: {
        "Accept": "application/json; odata=verbose",
    },
    success: function (data) {
        if (data.d) {
            var webUrl = data.d.GetContextWebInformation.WebFullUrl;
            alert(webUrl);
        }
    },
    error: function (err) {
       alert(JSON.stringify(err));
    }
);
1
  • With dataType: "json" it throws an error. If I remove dataType: "json" it looks like data is the HTML of the page located at the URL. Commented Jan 9, 2015 at 17:35

Your Answer

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

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