Using Office365 API's in MVC WebApps

Yesterday a new version of Office365 API Tools Preview has been shipped. A lot of things have changed in this new release.


I'll don't write all the new features and changes down manually instead I'll explain how to write an MVC WebApp which calls into Office365 and pulls out upcoming events from Exchange Online for the given user.

Before you start

Ensure that you've installed the latest Version of Office365 API Tools Preview you can either download the bits from or install/update it directly from within Visual Studio 2013 using the Extensions and Updates window.

When you've installed latest bits, you're ready to start

Setting up the project

After creating a new ASP.NET MVC App you can bring O365 APIs into play by adding a "Connected Service" to your project.

Within the "Add a connected Service" dialog you've to log in with your O365 identity, by default the App will be configured to target a single tenant. (The tenant your O365 user belongs to)

Let's say you're interested in consuming calendar entries from users calendar, so you've to request the read permission as shown in the following figure.


As soon as you hit ok, O365 API Tools will pull all the required nuget packages into your project and create a sample API implementation. Behind the scense your app (with the requested permissions will be installed within your Azure AD, to ensure that the app can request these permissions at runtime).

In order to display all events on the website, you've to update the HomeController's Index method to look like the following.

[gist id="e024c9e37b1167ea0c50" file="HomeController.cs"]

Last thing you've to do is actually creating the View. VS has created an index view by default, let's now change this one to print all the calendar entries.

[gist id="e024c9e37b1167ea0c50" file="Index.cshtml"]

First run

When you start the App for the first time, your App will be executed on your local IIS Express. Because we're printing all calendar entries directly to the index view. You'll be redirected to the Common Consent dialog, which takes care of authneticating users. (You may know this experience from O365 already).


After singning in O365 API's take care of dealing with your OAuth 2.0 Tokens, that will be used for any further call into Office365. Finally our events will be printed to the website


Let's review CalendarAPISample

Tools are generating an API Sample for the type of data you've requested. This is where acually things like Authentication and querying data happens.

[gist id="e024c9e37b1167ea0c50" file="CalendarAPISample.cs"]

Before you can query or post data to O365 you've to ensure that the user is authenticated using the EnsureClientCreated method. If the user isn't authenticated yet, the Common Consent dialog will be presented.

Depending on the kind of project, you'll either receive a native dialog or like in this sample the website will be shown.

Once the user successfully authenticated, we can query O365 by using a combination of O365 API and LINQ queries as shown in GetCalendarEvents.

Finally there is a SignOut method, which can be used to force logging out the current user.


Getting started with O365 API's targeting a single tenant or a single organization is really easy. Reading or Writing data is straight forward. Within my upcoming post I'll explain how to create Apps targeting multiple tenants using the Discovery Service which is offered by O365.


comments powered by Disqus