Use the Following Code in .aspx page (will you will add as a client web part) head section Script Tag to retrieve and set the query string value in a hidden field(in my example) when the page loads.
// Seperates & Returns the Query String from the Host URL.
function getParams(url) {
var idx = url.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = url.substring(idx + 1, url.length).split('&');
for (var i = 0; i < pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
// Seperating the Value Of Query String
// Id is the Query string name the you use in the URL.
var queryStringValue = unescape(params["Id"]);
return queryStringValue;
}
// Post Message function to communicate with the Host Page to get the Host URL.
(function () {
var getHostPageInfoListener = function (e) {
var messageData;
try {
// convert a JSON string to an object.
messageData = JSON.parse(e.data);
// Retrieves the HostPage URL of the HostPage
var myVar = messageData.hostPageURL;
// Calls the getParams Method to fetch the Query String.
var queryString = getParams(myVar);
var JavaScriptVar = queryString;
var hiddenQueryString = document.getElementById('HiddenQueryString');
hiddenQueryString.value = JavaScriptVar;
}
catch (error) {
// console.log("Unable to parse the response from the host page.");
return;
}
}
// Register the listener
if (typeof window.addEventListener !== 'undefined') {
window.addEventListener('message', getHostPageInfoListener, false);
}
else if (typeof window.attachEvent !== 'undefined') {
window.attachEvent('onmessage', getHostPageInfoListener);
}
// Send the host page a message
var hostPageMessage = {};
hostPageMessage.message = "getHostPageInfo";
var hostPageMessageString = JSON.stringify(hostPageMessage);
window.parent.postMessage(hostPageMessageString, document.referrer);
// console.log("Sent host page a message: " + hostPageMessageString);
})();
document.domainmanipulation to get around the error. Let me know if you need more information than that and I can post a full answer for you.document.domainwould be the app's URL.document.domainwould beapp.[domain]while the host was[domain]. Basically, it was a provider hosted app and I had control over the domain names. If it's an auto-hosted app, this method won't work, and I'm not sure what to do.