I am trying to get site collection URL using


but it is giving output as https://domain.-a08083bbaacc988.sharePoint.com/sites/sitecollectionname

but I want it as https://domain.sharePoint.com/sites/sitecollectionname that is without instance id

How to get site collection url?

2 Answers 2


From your SharePoint Hosted app what you can get context of host web and fetch the url of site collection You can use RequestExecutor for cross domain request as

 $.getScript(hostUrl + "/_layouts/15/" + "SP.RequestExecutor.js", GetSiteUrl);

and then fetch URL of sitecollection using bellow function

function GetSiteUrl() {
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var currentcontext = new SP.ClientContext()
    var hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
    var site = hostcontext.get_site();
    currentcontext.executeQueryAsync(function (s, a) {
        sitecollectionurl = site.get_url()


Hope this will help you!


In your App url there are various Query String Parameters as shown here. That already containts the site collection url.

The Query String paramter "SPHostUrl" will give you the site collection URl.

In order to get that URL you can use following code

function getQueryStringParameter(paramToRetrieve) {
var params =document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];

Just use this code as

hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl")

Now this hostweburl will contain your site collection URL. For code reference you can see here

  • but how can we predict the site collection url Aug 8, 2016 at 8:21
  • You don't need to predict. It will give the url of the site collection on which the app is installed Aug 8, 2016 at 8:27
  • I don't want host web url, I want to fetch site collection url (root of subsites ) Aug 8, 2016 at 8:28
  • Yes, the parameter I have specified it will give you what you want. please try once and then let me know. its working code I am daily used to SharePoint hosted app. Aug 8, 2016 at 8:30
  • You can just copy your full url and paste it in notepad and see the parameter "SPHostUrl" will contain your site collection url Aug 8, 2016 at 8:30

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.