I'd like to have access to sharepoint's query string from inside of my app. Since the app is hosted in an iframe, I tried just using window.top.location.search, but that gives me an error since the URL is different for my iframe vs. the parent window.

Is there a way I can see the query string of the SP url inside of my app? If not, how can I pass dynamic data easily to my app through a link, like I could with a query string parameter?

  • Did you get this resolved? I am wondering how to achieve the exact same thing. – Graham May 30 '13 at 1:09
  • I ended up changing how the site was hosted to be a subdomain of the SP site, and did some document.domain manipulation to get around the error. Let me know if you need more information than that and I can post a full answer for you. – Colin DeClue May 30 '13 at 14:01
  • How did this work with the app part? document.domain would be the app's URL. – EHorodyski Jul 5 '13 at 13:47
  • Correct, but I knew that document.domain would be app.[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. – Colin DeClue Jul 9 '13 at 16:37
  • did you find the resolution for this problem? – user18939 Aug 14 '13 at 21:24

The long and the short of it is, you really can't pass parameters to your app part via a query string. The app part is raised by way of a "redirect.aspx" page which gets rid of any query string params you might send it. Instead, it looks like what you need to do is utilize HTML5 to send a postMessage to the app page. More on how to do this here:



document.referrer should get you the URL for the page the iFrame is in including the querystring.

  • This doesn't work, because it gives an intermediate URL that redirects to the page inside the iframe. It's giving me http://spurl/_layouts/15/appredirect.aspx?redirect_uri= and then some encoded stuff at the end. – Colin DeClue Apr 11 '13 at 13:33

Did you try using window.parent?


  • If we use window.parent it your sharepoint hosted app, it will give you "Access Denied" error. – Mihir Jul 17 '14 at 5:30

Script to get the current page URL/pass the query string from the **SharePoint hosted apps**

var currentPageURL = (window.location != window.parent.location) ? document.referrer : document.location;

Script to get the current page URL/pass the query string from the **Provider hosted app you can use PostMessage API concept**

  • There's another answer that suggested this same thing. I left a comment on that talking about why that didn't work. – Colin DeClue Mar 24 '14 at 19:03

I came across with the same problem in my project where i need to fetch the query string when redirected to a page running provider hosted app and bring the data from the document library based on query string value(ID) and populate on form.

It is cross domain issue as we cannot access the host domain directly for security reasons.

But there is a work around for this situation.

We can use postMessage API to enable safe, cross-domain communication between windows or iframes.


  • Please add the details to your answer here. Your answer will be super helpful if the links breaks right now – Robert Lindgren Aug 11 '14 at 7:46

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.");

         // 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);

Add a Content Editor Web part on the page where you are going to add your provider hosted app and write the below code in Content Editor Web part Script tag

(function() {   

    var sendHostPageInfoListener = function (e) {
    var messageData;


        messageData = JSON.parse(e.data);

    catch (error)
        console.log("Could not parse the message response.");


    //Construct the return data to send to the app part

    var returnData = {};
    //returnData._spPageContextInfo = _spPageContextInfo;

    returnData.hostPageURL = document.URL;
    var returnDataString = JSON.stringify(returnData);

    e.source.postMessage(returnDataString, e.origin);
    //console.log("Sent app part iframe message: " + returnDataString); 

    // Register the listener

    if (typeof window.addEventListener !== 'undefined') {
        window.addEventListener('message', sendHostPageInfoListener, false);
    else if (typeof window.attachEvent !== 'undefined') {
        window.attachEvent('onmessage', sendHostPageInfoListener);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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