3

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?

6
  • Did you get this resolved? I am wondering how to achieve the exact same thing. Commented May 30, 2013 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. Commented May 30, 2013 at 14:01
  • How did this work with the app part? document.domain would be the app's URL. Commented Jul 5, 2013 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. Commented Jul 9, 2013 at 16:37
  • did you find the resolution for this problem? Commented Aug 14, 2013 at 21:24

7 Answers 7

1

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:

http://mysharepointinsight.blogspot.com/2013/08/how-to-pass-parameters-between-your-app.html

0

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

1
  • 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. Commented Apr 11, 2013 at 13:33
0

Did you try using window.parent?

https://developer.mozilla.org/en-US/docs/Web/API/Window.parent

1
  • If we use window.parent it your sharepoint hosted app, it will give you "Access Denied" error. Commented Jul 17, 2014 at 5:30
0

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**

1
  • There's another answer that suggested this same thing. I left a comment on that talking about why that didn't work. Commented Mar 24, 2014 at 19:03
0

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.

http://www.lifeonplanetgroove.com/use-postmessage-shim-give-sharepoint-client-app-parts-information/

1
  • Please add the details to your answer here. Your answer will be super helpful if the links breaks right now Commented Aug 11, 2014 at 7:46
0

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);
     })();
0

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;

    try
    {

        messageData = JSON.parse(e.data);

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

        return;
    }

    //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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.