Jul 23, 2012

Building ASP.NET MVC based SharePoint Cloud Apps

When you either create an auto-hosted or a provider hosted app, Visual Studio automatically generates an ASP.NET Forms project for you. Do you really want an ASP.NET Forms project? Many web developers would say no! They would prefer ASP.NET MVC. Anyway each of this two models has his advantages and disadvantages. Discussing this in detail would be out of scope for this post. Personally I think ASP.NET MVC and SharePoint Apps fit together really good. In this post I’m going to show step by step how to create an ASP.NET MVC based SharePoint app.

Create the SharePoint App

Create either an auto-hosted or provider hosted SharePoint 2013 App. For this example I’ve chosen auto-hosted. As development system I use a local Visual Studio with an Office 365 developer site.


Visual Studio will generates the following solution structure for you. Including a SharePoint App project and an ASP.NET Forms Web project.


We prefer ASP.NET MVC. So let’s add an ASP.NET MVC (.NET 4.0) project to the solution. The Azure tenant currently supports only .NET 4.0.



In the SharePoint App property windows choose the ASP.NET MVC project as that associated web project.


One of the most challenging tasks of app development is the authentication. To help you with this Microsoft added a Token Helper class to the auto-generated ASP.NET Forms project. We want use the Token Helper too, so let’s copy the TokenHelper.cs file to our ASP.NET MVC project.


If you want to, you can change the namespace of the TokenHelper.cs to fit the project default namespace.

To be able to build the project you need to add some new references to the ASP.NET MVC project.

  • Microsoft.IdentityModel
  • Microsoft.IdentityModel.Extensions (if you don’t find the reference, copy the path from the ASP.NET Forms project and choose browse in reference dialog)
  • System.IdentityModel

This is be able to build again. But it very likely that you also want to callback to SharePoint, so add the references for the ClientOM.

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.ClientRuntime

You have to set the copy local property of all the above references to true. 

We also need to enable SSL on our MVC project. If we would forget this, SharePoint would not send a Context Token. OAuth 2.0 requires SSL.


It is also useful to set the MVC project to “Don’t open page…”


Right click Controllers folders in the MVC project and add a new controller.


Add the following code to the controller:


Within the Views folder create a new sub folder called “Home”. Right click the folder and choose and add a new View:


Add the following code to the view:


Set the start page of our app to this view. To do this you have to switch to the code view of AppManifest.xml.


Now you can start the app for the first time. Hit F5, trust and start the app.


You have just created your first ASP.NET MVC based SharePoint App.

Note that during the first run Visual had added a Client Id and a Secret to the web.config of our ASP.NET MVC project.


When everything works as expected you can now delete the old ASP.NET Forms project.

Add SharePoint Look and Feel to the App

We are now going to add the ShareCoint chrome to our MVC site. Under Views->Shared modify _layouts.cshtml as follows:


Don’t panic, you can download the source code here.

Right click the app project and click deploy to deploy the app to Office 365. In the background tha ASP.NET MVC project will automatically provisioned to Windows Azure. The app should now look like this:


Wish you a lot fun with SharePoint Apps & ASP.NET MVC.



Jul 20, 2012

SPThumbMaker-Example for an Auto-Hosted Azure SharePoint App

To dig a little bit deeper into the possibilities of auto-hosted and provider hosted SharePoint 2013 apps, I’ve created a small example called “SPThumbMaker”. The app generates thumbnails for images that are stored in a picture library on the host site. To see how to get started with Auto-Hosted Azure apps read my previous post or the post Creating Provider Hosted Apps from my MVP mate Thorsten Hans.

Extend the Ribbon and the ECB to add the actions for the thumbnail generation

Ribbon Button “Create Thumbnails”


ECB Action


Elements XML of the two actions:


To only show the actions in picture libraries only I registered the actions on the list template type 109 (Picture Library). The “~remoteAppUrl” token points to the dynamically hosted ASP.NET site. Because my ASP.NET site must know for which image it should generate templates I’ve also appended the tokens “{ListId}” and “{ItemId}”. Beside the “~remoteAppUrl” token there is no difference to how you to SharePoint 2010 ribbon extension. But there this one restriction:
You can’t make use of JavaScript in the CommandAction. This is not allowed by design.

Get SharePoint Look and Feel on your ASP.NET Site

To integrate remote apps seamless into SharePoint the product team added the possibility to add the SharePoint chrome to our remote app.

Add the jQuery and ASP.NET AJAX Libraries


Dynamically load the “SP.UI.Controls.js” script. The scripts contains everything you need to render the SharePoint chrome on your site. This way you can load any other SharePoint script and CSS from within your app.


Render the SharePoint Chrome. You can customize the chrome via the options parameter. For example set the icon, title, add links…


Add a div container to render the chrome


Now the site header looks like this Smile


Server-side Thumbnail Generation

The generation works as follows:

I in the code behind of the ASP.NET form I extract the URL parameters, valid the SharePoint Context Token and get an OAuth AccessToken.


In the markup of the page I render image tags with a dynamically generated urls to a custom HttpHandler that generates the thumbnails.


I append to the URL all the parameters that I’ve got from the action (SPHostWeb, ListId, ItemId), the AccessToken and the size of the thumbnail.


In the HttpHandler I extract the query parameters and use the AccessToken to get a CSOM ClientContext to load the target  image from the host site and generate the thumbnail.


Finally the result looks like this


I hope this post helps to get a little bit better understanding of what you can do with auto-hosted and provider hosted apps.

You can download the source code here.

Jul 19, 2012

Create a Windows Azure Auto-Hosted App in SharePoint 2013

Wouldn’t it be awesome if you could deploy server side code to your office 365 tenant? Yes, it would! And with Windows Azure Auto-Hosted apps in SharePoint 2013 it is possible! This is amazing! I feel a little bit sorry for all the competitors. With the possibility to run server side code within Office 365 there are only few reasons to run SharePoint on premise. Especially midsize companies should consider really well to migrate to Office 365.

To get this working internally each Office 365 gets automatically an Azure account associated. More details about the architecture and billing will follow until the RTM.

In an azure auto-hosted app you can include:

  • Web Projects (Shared IIS Hosting)
  • SQL Azure Databases



  1. Create a Windows Azure Auto-Hosted App in Visual Studio 2013
  2. Deploy to SharePoint
  3. Add the App to a site
  4. SharePoint automatically provisions the Azure Website and the SQL Azure DBs that are included in the app package on the associated Azure tenant.

Execution Flow


  1. User clicks app “A”, SP loads app specific page “B”
  2. Content Area “B” loads Windows Azure page
  3. Azure executes Code
  4. Azure can interact with SharePoint using Client Object Model (CSOM) and OData Services.
  5. Azure app can utilize SQL Azure DBs
  6. Azure app can interact with external services “C”
  7. Azure app can interact with the host site.

Let’s give it a try…

Create a SharePoint 2013 App in Visual Studio 2012 (don’t forget to start Visual Studio as an admin).


Enter the name of the app, the URL of your development site and choose “Autohosted”. For this app you do not need a local SharePoint Installation! I’ve used a developer site within the SharePoint Online 2013 preview.


Visual Studio will generates two projects. “SPAutohostedApp” constains the AppManifest for your remote app and SPAutohostedAppWeb is an ASP.NET Forms that projects that hosts your ASP.NET code. The included class “TokenHelper.cs” will do all the OAuth 2.0 magic for you.


The AppManifest.xml the defines the startpage of your remote app. “~remoteAppUrl” will be replaced with the url of your dynamically hosted web application. In the AppPrerequsites is defined that your web application should be automatically provisioned to azure .


Default.aspx includes sample code how to handle OAuth 2.0 authentication with the help of “TokenHelper” and how to callback to SharePoint using CSOM.


Press F5.

Trust the app.


Launch the app.



In debug mode the app will not be auto provisioned to azure. Visual Studio starts IIS Express to host and debug your ASP.NET website locally. When you launch the app in SharePoint Online the app url refers to localhost and you can debug your code. This is really handy.


Deploy the App

Now we’re going to deploy the app instead of debugging.


Trust and launch the app.


Checkout the URL. The website has automatically be provisioned to Azure! Awesome!

Create An App Package

In order to publish the app to your company’s app catalog or to the public marketplace you can build an app package.




Jul 17, 2012

Apps in SharePoint 2013 - A New Developer Paradigm

As I first saw SharePoint 2013 I was really surprised. I had just expected some enhancements to the Farm and Sandboxed Solutions model, but the product team unveiled a completely new development model. This doesn’t mean that the old stuff will not work anymore, but the new model seems to be preferred for a couple of reasons.

Why the hell do we need a new development model?

Custom Code

Within the last few years SharePoint becomes the fastest growing server technology ever. Microsoft sold over 100 million SharePoint licenses. This fact made SharePoint very attractive for partners. Currently almost every Microsoft partner is developing SharePoint solutions – more or less successful. There is one problem, SharePoint development isn’t easy, it’s complex and error-prone. Learning it could take years. And the fact that each bad farm solution could break your whole SharePoint farm doesn’t make the situation better. Guess what Microsoft’s support case #1 for SharePoint is?

Sandboxed Solutions

Don’t want to talk much about Sandboxed Solutions. Sandboxed Solutions has been an approach to address the above mentioned issue with farm solutions and to get an extension model suited for hosted multi-tenancy environments like Office 365. But in many cases the model is to restricted. For example there are no off-box connections. Most the time you just use the sandbox to roll out some JavaScripts using ClientOM and Web Services. I believe that the sandbox solutions model will not succeed.


There is a highly increasing demand for SharePoint developers. But unfortunately there aren’t many good SharePoint developers and becoming a good SharePoint developer can take years. Currently there are only few SharePoint developers but about 10 million non SharePoint developers world wide.


HTML5, CSS, JS and railed development are going to dominate the next decade of web development. SharePoint is based on ASP.NET Forms. Most web developers don’t like ASP.NET Forms, because it produces ugly HTML.

Identity Management

In a more and more connected and service oriented world, identity management is from vital importance. It seems that OAuth 2.0 have a promising future.

The App Model

Microsoft’s solution for all this issues is the new App Model. The App model in SharePoint 2013 is based on web standards: HTML 5, CSS, JS, OAuth 2.0 and OData. The dev model is similar to dev models like the one from Facebook. This opens SharePoint development to about 10.000.000 million potential developers.
Microsoft invested a lot in making most of the SharePoint API functions remote accessible. A remote interface is much easier to maintain and more secure. Developers will not be able to do all the hacks and workarounds they did before (SharePoint developers usually spend most their time doing exactly this ;)
The new paradigm is: no server-side code on the SharePoint box.
This is not something bad, this absolutely needed and addresses the issues with farm and sandboxed solutions. The idea is, let you server-code run on a remote box and callback to SharePoint through the OData based remote API. Because OData is a standard it doesn’t matter, which programming language do you use. C#, Python, Ruby, Java, PHP, NodeJS, it’s up to you. I really appreciate this strategic decision. SharePoint isn’t a real application platform. SharePoint is a portal, a door to the world of information within your company. It is a collaboration platform. On the other hand Azure is a real application platform. But both together are a perfect match.

Different Types of Apps

SharePoint-hosted apps

Provisions an isolated sub web on a parent web. The app webs site gets it’s own unique sub-domain (browser cross domain policy). This is important to separate one app from each other app. Use lists, out-of-box web parts. No server-side code allowed, client JavaScript only.

Provider-hosted apps

Bring your own server hosting infrastructure. Use the programming language of your choice and callback to SharePoint via the OData API. There is also an option to create remote event receivers.

Autohosted apps

Windows Azure Web Site and SQL Azure Database provisioned automatically when app is installed.

Hybrids between SharePoint-hosted and self-hosted apps

You can combine SharePoint-hosted apps with remote apps.

How does an App looks like?

The main part on app is the app manifest:
<?xml version="1.0" encoding="utf-8" ?>

<App xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest (http://schemas.microsoft.com/sharepoint/2012/app/manifest)"







    <Title>Local Theater</Title>



    <RemoteWebApplication ClientId="ca81d876-8525-44a8-8a60-e02ee79a4a6e" />



The manifest contains an unique app id, the title, permissions and a start page. The example shows how to register a remote app. {StandardTokens} appends some standard url parameters to the request like the url of the SharePoint host site. The url of the SharePoint host site is important for callbacks from the remote app to SharePoint.


Microsoft also is going to introduce a commercial public marketplace for SharePoint and Office apps (Office apps are similar to SharePoint apps and will bring a new and unified web development model to the Office rich clients and web apps). 100 million potential SharePoint apps customers, or even 750.000.000 potential Office apps customers sounds very promising. Beside the public marketplace there is also the option to have a private corporate app catalog.


I think the SharePoint product group did a really good job with the new app model, the marketplace and the current SharePoint strategy. They did some really clever decisions!´

You can also check out the blog from Thorsten Hans. He’s also going to blog a lot about the new SharePoint app model…

SharePoint 2013 Team Site Enhancements (First Look)

Today Steve Ballmer unveiled the customer preview of the new Microsoft Office. There really tons of new things to talk about. I start with the:

End User Perspective

Something that we geeks often forget is that SharePoint is a product for end user not for IT-Experts. A little bit of SP history:
  • SharePoint 2003 was kind of a prototype.
  • With SharePoint 2007 the developers got a professional extension/development model (Features & Solutions).
  • With SharePoint 2010 the IT-Pros got the Service Applications
  • And finally with SharePoint 2013 it seems the end users can use it ;-)

A first look:

It’s not a big surprise that they are now some Metro tiles on a team site. But there are more important details…
It is now really easy to invite people to a team site. You have only click to “SHARE” and enter the people you would like to invite.
(Under show options you can select a group or permission level. In the beta the default setting is the member group)
And now you can easily “FOLLOW” team sites, documents, persons and tags.
One of the most desired features in SharePoint has been Drag & Drop support. Now we have it:
SP2013 Drag & Drop Demo
(Doesn’t require an ActiveX plugin. I did the demo with Firefox. With IE you need a local Office 2013)
As mentioned before sharing and following a document is as easy as to share the whole site:
You can also share a file with to unauthenticated users in SP Online (nightmare for IT-Pros ;)
Search files in a Document Library.


There is now a OneNote Notebook on default Team Site. This is really great to take notes in a very rich way with OneNote Client (Tablets, Audio, Ink…), but you have also as fallback the OneNote WebApp.

Everything is an App

Something that could be a little bit confusing for users is that now everything is an App. Now they don’t add List or Libraries anymore (at least regards naming). Now they add List- and Libraries-Apps.

Site Newsfeed

When you activate a site feature called “Site Feed” you get a Facebook-like Newsfeed on your site. I really like that feature. This has the potential to kill tons of emails. Until now employees (including myself) often use emails for this kind of chatty conversations.
Even this aren’t technical revolutions, I believe SharePoint 2013 will be a major step forward regards end user adoption.
And there is a lot more to discover: Team Mailboxes, Community Sites…