I am creating a Provider hosted app. In that I have created two different .aspx pages. I want to redirect to the other page. But I am not able to find it out how to redirect.

When I check my AppManifest file there is a URL to show which page to be load on start up. It is like follow.


I tried to put url like this in Page.Response.Redirect(""); but m not able to do this.

  • I am putting Redirecting code to code behind on a Button click – Rahul Gokani May 24 '13 at 8:42
  • I mean to say I have tried to put the url "~remoteAppUrl/Pages/PollQuestionView.aspx?{StandardTokens}" like this but it is not helping, In other page I have to pass the Query string with standard tokens. – Rahul Gokani May 24 '13 at 8:53
  • I have to pass url in this format "/Pages/PollQuestionView.aspx?SPHostUrl=http%3a%2f%2fspmain%3a3865&SPLanguage=en-US&SPClientTag=0&SPProductNumber=15.0.4420.1017" but it how to put this tokens in url and where to get those tokens that i don't know. – Rahul Gokani May 24 '13 at 8:56
Response.Redirect(TokenHelper.GetAppContextTokenRequestUrl(sharePointHostUrl, Server.UrlEncode(targetUrl)));

The app redirect page


Why not just use a server relative Url?

Like: Page.Response.Redirect("PollQuestionView.aspx")

The standard tokens are replaced at compilation time in for example the AppManifest. They will NOT be replaced in your code at runtime. So if you want to pass them on you would need to get them from Page.Request and add them to the Redirect Url.

Use Page.Request.Querystring["Name of parameter"] to get the value of a specific querystring parameter.

  • no its not working. I'll need the standard tokens with my url otherwise it wont work. – Rahul Gokani May 24 '13 at 8:52
  • Yes you are write. But how could I get these tokens from Page.Request?? – Rahul Gokani May 24 '13 at 8:58
  • I want url like /Pages/PollQuestionView.aspx?SPHostUrl=http%3a%2f%2fspmain%3a3865&SPLanguage=en-US&SPClientTag=0&SPProductNumber=15.0.4420.1017 – Rahul Gokani May 24 '13 at 8:59
  • Updated with how to retreive from querystring – Robert Lindgren May 24 '13 at 9:01
  • 1
    @RahulGokani you can use Request.Url.Query to get query string back. – Muhammad Raja May 24 '13 at 9:10
Use this instead, string url = "~remoteAppUrl/Pages/PollQuestionView.aspx"; string queryString = "SPHostUrl=http%3a%2f%2fspmain%3a3865&SPLanguage=en??-US&SPClientTag=0&SPProductNumber=15.0.4420.1017"; SPUtility.Redirect(url, SPRedirectFlags.Default, Context, queryString); [How to use SPUtility.Redirect][1] [MSDN reference][2]

This should work,

Response.Redirect(TokenHelper.GetAppContextTokenRequestUrl(sharePointHostUrl, Server.UrlEncode(targetUrl)));
  • In SharePoint App Development I cant use SPUtility. I have to use Client Context Model to develop this... – Rahul Gokani May 24 '13 at 9:11

You can use the current SharePointContext instance instead of redirecting back to the SharePoint host and then being redirected once again to the app to get a new context (as suggested by Rahul).

Add these utility methods to the auto-generated SharePointContext.cs file (at around line 277 which is where the SharePointContext class definition ends. Don't forget to add 'using System.Linq;' and 'using System.Collections.Specialized;')

public void AddStandardTokensToQueryString(NameValueCollection queryString)
    if (queryString == null)
        throw new ArgumentNullException("queryString");

    if (spHostUrl != null)
        queryString[SPHostUrlKey] = SPHostUrl.AbsoluteUri;

    if (spAppWebUrl != null)
        queryString[SPAppWebUrlKey] = SPAppWebUrl.AbsoluteUri;

    if (!string.IsNullOrWhiteSpace(SPLanguage))
        queryString[SPLanguageKey] = SPLanguage;

    if (!string.IsNullOrWhiteSpace(SPClientTag))
        queryString[SPClientTagKey] = SPClientTag;

    if (!string.IsNullOrWhiteSpace(SPProductNumber))
        queryString[SPProductNumberKey] = SPProductNumber;
public String GetStandardTokensQueryStringValue()
    NameValueCollection qs = new NameValueCollection();
    return String.Join("&",
        qs.AllKeys.Select(a => a + "=" + HttpUtility.UrlEncode(qs[a])));

How to use:

protected void btnSubmit_Click(object sender, EventArgs e)
    SharePointContext spContext = 
    Response.Redirect("PollQuestionView.aspx?" + 
        spContext.GetStandardTokensQueryStringValue(), true);

use window.location.href = url; for sharepointhosted App and Client API.

  • It would be great if you add some more explanation to your answer. It will be good in reading and understanding. – Asad Refai Aug 4 '15 at 9:49

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.