Office 365 API Development in Visual Studio
However, Visual Studio 2013 does not include a default project template for Office 365 APIs to create new Visual Studio projects that provides you the required code to kick start your Office 365 development.
The code to authenticate your application will change depending on the type of the Visual Studio project, such as, an ASP.NET MVC Application or Windows Store or Windows Phone projects.
You can find code samples for almost every project type in Office Developer GitHub.
In this post, let us see how to quickly clone the ASP.NET MVC project template from GitHub to build a web application that consumes Office 365 APIs.
Below are the two ASP.NET MVC web application project templates available at Office Developer GitHub:
What You Need
To successfully build and execute this sample, you will need:
- Visual Studio 2013
- Office Developer Tools for Visual Studio 2013
- Office 365 Developer Subscription
Choosing Between Building Single Tenant and Multi-tenant Web Applications
If the application you are building is deployed internally for your organization and allows only your organization users to sign in and use the application, then you should build a single tenant application.
If the application you are building is deployed internally for your organization but also allows external users, such as vendors to sign in and use the application, then you should build a multi-tenant application.
Clone GitHub Office 365 API Web Application in Visual Studio
Visual Studio 2013 supports connecting to Git servers. As the project templates are hosted in GitHub, Visual Studio 2013 makes it easier to clone projects from GitHub.
The steps below will describe how to clone Office 365 API web application project in Visual Studio from Office Developer GitHub.
1. Open Visual Studio 2013
2. Switch to Team Explorer
3. Team Explorer provides options to clone Git repositories
4. Click Clone under Local Git Repositories, enter the clone URL for the web application project and click Clone
- Clone URL for single tenant web application:
- Clone URL for multi-tenant web application:
Visual Studio will clone the Git repo in your local development machine.
5. Once the project is cloned, double click on the repo
6. Double click the project solution which is available under Solutions
7. Visual Studio will open the cloned project
8. Switch to Solution Explorer and build your project
- Visual Studio will download the NuGet packages and compile the project
- Ignore any errors at this point (as we will fix in the steps below)
Integrate Your Application with Office 365 APIs
Office 365 applications use Azure Active Directory (Azure AD) to authenticate and authorize users and applications respectively. All users, application registrations, permissions are stored in Azure AD.
Using the Office 365 API Tool for Visual Studio you can configure your web application to consume Office 365 APIs.
- In the Solution Explorer window, right click your project -> Add -> Connected Service.
- A Services Manager dialog box will appear. Choose Office 365 -> Office 365 API and click Register your app.
- On the sign-in dialog box, enter the username and password for your Office 365 tenant.
- After you’re signed in, you will see a list of all the services.
- Initially, no permissions will be selected, as the app is not registered to consume any services yet.
- Select Users and Groups and then click Permissions
- In the Users and Groups Permissions dialog, select Enable sign-on and read users profiles’and click Apply
- Select Contacts and then click Permissions
- In the Contacts Permissions dialog, select Read users’ contacts and click Apply
- Click Ok
After clicking OK, Office 365 client libraries (in the form of NuGet packages) for connecting to Office 365 APIs will be added to your project.
In this process, Office 365 API tool registered an Azure AD Application in the Office 365 tenant that you signed in the wizard and added the Azure AD application details to web.config.
Azure Active Directory Authentication Library (ADAL) is used to authenticate your applications to Office 365 APIs. The web application project is already configured to use ADAL.
If you are building single tenant application:
There is one extra configuration required if you are building a single tenant application.
In your web.config, update the TenantId value to your Office 365 tenant Id where the application is deployed.
To get the tenant Id of your Office 365 tenant:
- Log in to your Azure Portal and select your Office 365 domain directory.
NOTE: If you are unable to login to Azure Portal using your Office 365 credentials, You can also access your Office 365’s Azure Portal directly from your Office 365 Admin Center.
- Now, in the browser URL, locate the GUID. This will be your Office 365 tenant Id.
- Copy and paste it in the web.config where it says “paste-your-tenant-guid-here“ :
- <add key=“ida:TenantId“ value=“paste-your-tenant-guid-here“ />
Debug the Web Application
Hit F5 to debug your web application
NOTE: If this is a new development environment, Visual Studio may prompt to configure IIS SSL certificate. Click Yes (twice) to continue.
1. Visual Studio will launch the browser with the web application.
2. Click Sign in at the top right corner and sign in to your Office 365 tenant where you registered your web application.
3. If your application is a multi-tenant application, then you should see a consent screen:
NOTE: Since only your organization users are allowed to sign-in in a single tenant application, there is no need to consent.
Once successfully signed in, Azure AD will display the consent screen which shows the different permissions your web application is requesting.
Click Accept to continue
5. Now you should see your email address displayed at the top, instead of sign in.
6. Click on My Contacts
7. My Contacts page will retrieve your Exchange contacts (if any) from your tenant
You now have a web application project that you can customize and integrate Office 365 APIs.
Quick Look at the Code
The authentication startup class, App_Start/Startup.Auth.cs in the project contains the startup logic for Azure AD authentication.
The sample uses a persistent ADAL token cache that uses a database for its token cache. You can see the token cache implementation in the following files:
The sign in and sign out controls are already added to the views. You can find them underViews\Shared folder. 1. _LoginPartial.cshtml is the partial view that renders the Sign In and Sign Out actions. 2. _LoginPartial.cshtml is then rendered in _Layout.cshtml 3. TheAccountController.cs has the required methods for sign in and sign out.
Applying Authorize attribute to all controllers in your project will require the user to be authenticated before accessing these controllers. To allow the controller to be accessed anonymously, remove this attribute from the controller. If you want to set the permissions at a more granular level, apply the attribute to each method that requires authorization instead of applying it to the controller class.
You can now write code to call an Office 365 API in your web application. You can apply the Autorize attribute to the desired controller or the method in which you wish to call Office 365 API.
ContactsController.cs describes how to interact with the Office 365 API discovery service, get the endpoint URI and resource Id for Outlook Services and then query users’ contacts.
Feel free to leave a comment below if you have any questions!
If you have a product/feature request, make sure you post your idea here!