in Office 365 API

Office 365 APIs: Getting Started with Building Windows Apps

Office 365 API Development in Visual Studio

Office 365 API Tools for Visual Studio 2013 helps you to configure and integrate Office 365 APIs for your existing application and adds the required Office 365 API SDKs to your project.

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:

  1. Visual Studio 2013
    1. If you do not have Visual Studio 2013, you can download and install the free Visual Studio Community Edition
  2. Office Developer Tools for Visual Studio 2013
  3. 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

VS2013-Team-Explorer

4. Click Clone under Local Git Repositories, enter the clone URL for the web application project and click Clone

  • Clone URL for Windows app:
    • https://github.com/OfficeDev/O365-Windows.git

clone-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

windows-app-cloned

6. Double click the project solution which is available under Solutions

windows-app-solution

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)

nuget-restore

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.

  1. In the Solution Explorer window, right click your project -> Add -> Connected Service.
  2. A Services Manager dialog box will appear. Choose Office 365 -> Office 365 API and click Register your app.
  3. On the sign-in dialog box, enter the username and password for your Office 365 tenant.
  4. After you’re signed in, you will see a list of all the services.
  5. Initially, no permissions will be selected, as the app is not registered to consume any services yet.
  6. Select My Files and then click Permissions
  7. In the My Files Permissions dialog, select Read users’ files and click Apply
  8. 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

windows-app-debug

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.

windows-app-consent-screen

Click Accept to continue

5. Now you should see your files load in the list box

windows-app-myfiles

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.

GetAccessToken Method

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!

Happy Coding!

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.

  1. Hello Chaks!

    My name is Michael Matveev and I’m a developer working on OS33 company. I’m investigating different ways to access to OneDrive for Business. I did some tests using Office 365 API and SharePoint CSOM both. It seems that Office 365 API doesn’t provide enough functionality (for our purposes). Could you clarify some points:
    1. Current version of Skydrive pro 2013 uses Sharepoint API. Which API will be used to access to OneDrive for Business in future? Will Office 365 API replace Sharepoint API completely or both methods will be supported?
    2. Is it planning to support versioning and check in/check out functionality in Office 365 API?
    3. What we actually need is a working with file custom attributes. On our side custom attributes are used to maintain the file states during long running operation. It is any plans to introduce custom attributes functionality into Office 365 API?

    • @Michael – The Files API does allow you to browse and manage users’ file in their Onedrive for Business. That said, this Files API does not provide SharePoint functionality such as document check-in, check-out and versioning. You will have to use SharePoint REST APIs.

  2. One more question. I’m trying to upload 1.83 GB file using Files API, but I getting out of memory exception. (possible because of 2GB limit for object size in CLR).

    public async Task UploadFileAsync(String localPath, String remotePath = null)
    {
    String name = localPath.Split(‘\\’).Last();
    var newFile = new O365.File
    {
    Name = name
    };

    var sharePointClient = await _authenticationHelper.CreateSharePointClientAsync();

    if (remotePath != null)
    {
    var parentFolder = await sharePointClient.Files.GetByPathAsync(remotePath);
    await sharePointClient.Files[parentFolder.Id].ToFolder().Children.AddItemAsync(newFile);
    }
    else
    {
    await sharePointClient.Files.AddItemAsync(newFile);
    }

    using (FileStream fileContent = new FileStream(localPath, FileMode.Open))
    {
    await sharePointClient.Files.GetById(newFile.Id).ToFile().UploadAsync(fileContent);
    }
    }

    Another way is using method SaveBinaryDirect from Microsoft.SharePoint.Client.File but it doesn’t work also (throw OutOfMemoryException).

  3. Hello,
    When I execute your project after make all changes you describe at the top of the post I get an error at the opening of the connection window (Unable to connect at the service – It is currently unable to connect you to the service what you need. Verify your network connection or try again later).
    Sorry for my little english I’m french developer.
    Thank you.

  4. Does not work!

    “Unspecified error

    Cannot find a resource with the given key.”

    This happens when I click “Get my Files”

    Any idea?