blog.scoreman.net

Archive for February, 2014

SharePointContext null in postback

Monday, February 24th, 2014

If you are experiencing issues with the SharePointContext class returning NULL when you call SharePointContextProvider.Current.GetSharePointContext(HttpContext) in a postback you may what to check that the context is initialized on the first page load. This can be done for example by calling GetSharePointContext in Page Load event or SharePointContextProvider.CheckRedirectionStatus in Page PreInit.

The reason for this is that the SharePointContext class is dependent on parameters (AppContext, AppContextToken, AccessToken and SPAppToken) being in the request as form or querystring parameters. If present SharePointContext will store values in session and cookie for use in future postbacks. Therefor if SharePointContext is not used at all on initial load all dependent parameters will get lost in postback.

It’s pretty easy to reproduce this, just create a new provider-hosted app using the ASP.NET MVC Form Application template. Remove any code in Page_PreInit and move the example code in Page_Load to a button click event that fires on postback. Debug application and notice that GetSharePointContext(HttpContext) returns NULL in button click event.

Get term store in SharePoint Online

Thursday, February 13th, 2014

This is just ta small tips when working with taxonomy client-side object model and SharePoint Online. You will find lots of examples like the one below online:

var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStores = taxSession.get_termStores();
var termStore = termStores.getByName("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");

The important thing to note is that the name of the term store (something like Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==) can and will change over time. This can happen if the service (Office 365) creates a new Managed Metadata Service Application or even fails over to another data center. To prevent the code from crashing a better approach could be to use getDefaultSiteCollectionTermStore:

var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStores = taxSession.get_termStores();
var termStore = taxSession.getDefaultSiteCollectionTermStore();