Fixing the Page Reload Issue for SharePoint ASP.NET MVC Provider Hosted Addins


If you launch a SharePoint/Office365 provider hosted addin using a direct URL to the app, rather than using the site contents page/applauncher, either as a dialog or fullpage immersive – you may notice that after your addin displays in the browser, the page is automatically reloaded.

First, in this scenario because you [probably] won’t have an access token, nor a context token, the MVC SharePointContext filter will redirect you back to the SharePoint site hosting the addin, specifically the AppRedirect.aspx page.

The AppRedirect.aspx page, after taking care of authentication, will POST you back to your addin with the SPHasRedirectedToSharePoint=1 querystring value.

At this point, you’re back in your addin and alls well and good, except that by default, your MVC addin will include the spcontext.js file, which is provided by project wizard.

This file is trying to be helpful, by adding the SPHostUrl querystring values to <A/> elements found on your page.

What it also does is remove the SPHasRedirectedToSharePoint=1 from the browser URL by modifying window.location.search. It does this to ensure that bookmarks to the addin are set correctly and don’t include this querystring value.

Unfortunately, changing window.location.search also causes the browser to reload the page.

There is a simple fix for this, which is to use the session history management capability provided by modern browsers (not IE 9 and below) which allows you to modify the browser URL without causing a page reload.

You can get an updated spcontext.js from the gist below, the changes are at the end of the file in the ensureSPHasRedirectedToSharePointRemoved() function.

 

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s