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 Windows App project template from GitHub to build a Windows app that consumes Office 365 APIs.
Below is the Windows app 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
Clone GitHub Office 365 API Windows App 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 Windows app:
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 My Files and then click Permissions
- In the My Files Permissions dialog, select Read users’ files 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 Windows app project is already configured to use ADAL.
Debug the Windows App
Hit F5 to debug your windows app
1. Visual Studio will launch the app
2. Click Get My Files button
3. Sign in to your Office 365 tenant where you registered your Windows app
3. You should see a consent screen
Once successfully signed in, Azure AD will display the consent screen which shows the different permissions your windows app is requesting.
Click Accept to continue
5. Now you should see your files load in the list box
You now have a Windows app project that you can customize and integrate Office 365 APIs.
Quick Look at the Code
AuthenticationHelper.cs constructs the code to authenticate and acquire tokens from Azure AD for Office 365 resources. In this sample, we acquire token for My Files resource.
In order to acquire tokens, we first query Office 365 Discovery Service to discover the app capabilities. As we configured the app to request My Files permissions, Office 365 discovery service will return My Files capability’s information such as the Resource Id and Endpoint URI. With this, we then acquire access toke for My Files resource.
Storing Last Tenant Id
As device applications are by default multi-tenant applications, the app initially does not know which user from which tenant is signing into the app until the user successfully signs and consent the app.
NOTE: Multi-tenant applications allow external Office 365 users to sign in and use the application. If the app is published to Windows Store, users can download the app, sign in to their Office 365 tenants and use the application. Azure AD will register the Windows application on behalf of the user in the respective tenant of the signed-in user.
Once consented, we can then get the signed-in users’ tenant Id which we store in the App settings. We can then use this Tenant Id to initialize authentication context next time the user opens the app or makes a request.
This eliminates the need for the user to consent every time as opposed to only once when the app is accessed by the user for the first time (until it is required, which is when the access token expires).
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!