MVC, SharePoint Apps and SharePointContextFilter

By | 2013-08-25

Visual Studio 2013, in RC now, comes with MVC App Templates for SharePoint 2013 and SharePoint Online. The MVC template gives you several different layouts. Empty all the way to some pre-configured application.


The best part, with this new MVC Template comes a pair of helper classes: [SharePointContextFilterAttribute] and [SharePointContext].

“These APIs wrap a series of steps for the web project to seamlessly communicate to SharePoint in a single call, so that the logic you need to implement is much simpler.

  • The SharePoint Context Filter attribute performs additional processing to get the standard information when redirected from SharePoint to your remote web application, such as Host Web Url.
    It also determines whether the app needs to be redirected to SharePoint for the user to sign in (in case of bookmarks).
    You can apply this filter either to the controller or to a view.
  • SharePoint Context classes encapsulate all the information from SharePoint so that you can easily create specific contexts for the app web and host web right away and communicate with SharePoint.”

Sample use in c#:

[SharePointContextFilter] // just add this attribute to your method
public ActionResult MyAction() {
    var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
    using (var clientContext = spContext.CreateUserClientContextForSPAppWeb()) {
        if (clientContext != null) {
                    // do SharePoint Magic!

Go to Visual Studio 2013 (Release Candidate)

See Introducing MVC support for Apps for SharePoint

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.