0

I've got a sharepoint site with multiple site collections.

/ site collection:

http://mysite/    <--- root site in `/` site collection
http://mysite/subsite1
http://mysite/subsite2

/sitecol1 site collection:

http://mysite/sitecol1/   <-- root site in `/sitecol1` site collection
http://mysite/sitecol1/mysubsiteinsitecol1
http://mysite/sitecol1/mysubsiteinsitecol2

Using rest api, how can I given http://mysite/sitecol1/mysubsiteinsitecol2 determine what root site colleciton it is from?

UPDATE:

Forgot to mention - I need this to work on SharePoint 2010, 2013 and 2016.

2 Answers 2

1

You can use REST API to get to the root site. Try this below endpoint in your browser. Append this after your site URL -

/_api/site/rootweb?$select=Title,ServerRelativeUrl,URL

The URL value will give your root site collection

1
  • doesn't work on sharepoint 2010. works on sharepoint 2013 and 2016 however. Commented Apr 26, 2018 at 18:44
0

You can make use of the _spPageContextInfo object to get the URL making use of the siteAbsoluteUrl property as:

var siteUrl = _spPageContextInfo.siteAbsoluteUrl

Another way to determine the site collection url is to make a JSOM call :

var clientContext = new SP.ClientContext();
var site = clientContext.get_site();
clientContext.load(site);
clientContext.executeQueryAsync(function(){ 
    console.log(site.get_url()); 
},
function(){
    console.log("something went wrong");
});
5
  • I'm not familiar with JSOM. but I'm sure that just comes down to REST calls when all is said and done? Commented Apr 27, 2018 at 13:59
  • yes internally its all REST calls. Just drop this code inside script tag and it should work Commented Apr 27, 2018 at 15:53
  • I'm on a linux machine with no access to the SharePoint instance. Can you please run this in your browser with devtools network tab enabled then send me the REST calls the browser made to get the info? Commented Apr 27, 2018 at 17:01
  • I have access to SPO only. The REST call in online will be different from OnPrem. Just paste the code in the browser console on a standard sharepoint page, it will work. Same goes for the _spPageContextInfo, just type that in console, it will give you the properties Commented Apr 27, 2018 at 17:30
  • here is what i got pastebin.com/x0zzKm7k but when i try to make this call directly from postman, i get 401 error every time Commented Apr 27, 2018 at 18:21

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.