6

I'm developing a Sharepoint 2013 Add-in, and I need to retrieve the query string of the original request.

Users are linked to our add-ins from emails and we need to provide some context. The add-in is requested like this: https://x.sharepoint.com/MyAddIn?p=10

Is it possible to retrieve the value of p in my add-in?

Tommy Jakobsen
  • 2,323
  • 6
  • 39
  • 66

2 Answers2

2

You could use the following code snippet:

Uri myurl = new Uri(Request.QueryString["SPHostUrl"]);
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("p");

or use

string param1 = Request.QueryString["p"];

If you want to this via JS, then go on with this

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

 var sProp = decodeURIComponent(getQueryStringParameter("StringProperty1"));     
 document.write('Value of StringProperty1 : ' + sProp + '</br>');
STORM
  • 4,005
  • 11
  • 49
  • 98
  • 2
    The SPHostUrl does not contain the query strings of the original request. When requesting Sharepoint add-ins, the user is redirected in the process. – Tommy Jakobsen Jan 16 '16 at 13:17
1

The SPHostUrl definitely shouldn't contain your parameter, but have you tried @STORM 's other suggestion: string param1 = Request.QueryString["p"];

Also I think it may be possible to avoid the redirect if you append the built-in add-in query string to the URL you are sending your users like so: http://<your-app-prefix>.domain.com/<Add In>?SPHostUrl=<host url>&SPAppUrl=<app url>&...&p=10

The add-in's that I've worked on send user's emails with links that require context as well and this method has worked for me.

If this doesn't work, is your add-in a provider-hosted or sharepoint-hosted add-in. This extra info may be helpful.

nerdondon
  • 26
  • 1
  • 3
  • Request.QueryString uses Request.URL underneath (the "current URL"), and not the one originally requested before the redirect. I will have to test your second method, but I would prefer not having to include SPHostUrl etc. because it generates some very long URLs. – Tommy Jakobsen Jan 18 '16 at 18:44
  • The add-in is a hosted add-in. – Tommy Jakobsen Jan 18 '16 at 18:44
  • I do most of my work on-prem, so Im not sure where this re-direct is coming from. My understanding is that the app requires the built in query string in order to validate the user context and that is why there is a redirect occurring for you. so there may be no way around the massive url string.i would also suggest you make sure that the link you give your users is pointing directly to the page location in the **app web** and _not_ the **host** web. this may also be the cause of the redirect. i dont think thats much of a problem since you can just use an `a` tag in an email to present the link – nerdondon Jan 19 '16 at 04:03
  • I was hoping for a way of passing the query string so the add-in can be requested like described in my post. – Tommy Jakobsen Jan 19 '16 at 17:55