I have a on premise sharepointed 2013 application. On the home page, there are several links to different pages within the application itself...such as

<a href="User.html">User</a>

However, when I click on this, I lose the HostWebURL and the AppWebURL in the url.

this contains vital information for making various ajax calls. Is there a way to maintain these query string parameters across all pages when someone navigates?

  • The only way to maintain them is to actually place them into the destination URLs before the browser is sent to that page. You could write javascript to find every link and add the current page's querystring to each URL or you could try intercepting each link click and add it at that time. Keep in mind this would not work for any pieces of javascript that navigate directly to a new page, only for clickable links in the page that you can find. You would also have to look out for dynamically generated content to make sure it was monitored also.
    – jfriend00
    Apr 8, 2014 at 20:12

2 Answers 2


I'm not sure how much of a hack this is, but we got tired of the HostWebUrl; So we edited the SharePointContext.cs:

  1. On Home controller we save the SPHostUrl if provided.
  2. Where SharePointContext.cs tries to resolve the host url from HttpContext.Request.QueryString, if the QueryString does not contain the SPHostUrl we simply read it from the cookie.

We decided to use cookies, and to create our own because that was least work, and most importantly least editing to SharePointContext.cs (the method accept Request, so Session is not easily available). All in all a few lines of code in one single place. And it works great between production and development.

HomeController Index:

public ActionResult Index()
    var host = HttpContext.Request.QueryString["SPHostUrl"];
    if (!string.IsNullOrEmpty(host))
        HttpContext.Response.AppendCookie(new HttpCookie("SPHostUrl", host));


    public static Uri GetSPHostUrl(HttpRequestBase httpRequest)
        var host = httpRequest.QueryString[SPHostUrlKey];
        if (string.IsNullOrEmpty(host))
            var cookie = httpRequest.Cookies.Get(SPHostUrlKey);
            if (cookie != null) host = cookie.Value;
        var spHostUrlString = TokenHelper.EnsureTrailingSlash(host);

If you have more than one instance of the same app in a farm and the user is using both at the same time you will overwrite your cookie...

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.