Monday, 9 February 2015

How to prepare Host-Web-Url and App-Web-Url, for SharePoint hosted app, that works for both root site and sub-sites?

$(document).ready(function () {
sharePointReady();
});

function sharePointReady() {
var temphostweburl =
decodeURIComponent(
getQueryStringParameter('SPHostUrl')
);

domainUrl = temphostweburl.substring(0, temphostweburl.indexOf("/sites"));
var tempHostUrl = temphostweburl.substring(temphostweburl.indexOf("/sites/") + 7, temphostweburl.length);

var splitedHostArr = tempHostUrl.split("/");

if (splitedHostArr.length > 1)
{ hostweburl = domainUrl + "/sites/" + splitedHostArr[0]; }
else
{ hostweburl = temphostweburl; }

appweburl =
decodeURIComponent(
getQueryStringParameter('SPAppWebUrl')
);

 
 
scriptbase = hostweburl + '/_layouts/15/';
$.getScript(scriptbase + 'SP.Runtime.js',
function () {
$.getScript(scriptbase + 'SP.js',
function () { $.getScript(scriptbase + 'SP.RequestExecutor.js', execCrossDomainRequest); }
);
}
);
}


function getQueryStringParameter(param) {
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] == param) {
return singleParam[1];
}
}
}

No comments:

Post a Comment