Consuming the Microsoft Graph APIs in Xamarin.Forms

Mayur Tendulkar

It’s highly likely that you’ve used at least one service powered by Microsoft Azure. Services such Mail, People, Outlook, OneDrive for Business, and SharePoint all depend on Azure for availability, scalability, reliability, and robust security. Until recently, if we wanted to interact with any of these services, we had to work with several separate APIs and SDKs.

Recently, however, Microsoft released a unified API to access all of these services, creating “one endpoint to rule them all” and making integrating with these services easier. The brand new Graph API is all about connecting the dots between these services and fetching contextual data with a common SDK available on all platforms that support .NET.

Step 1: Register the App and Authenticate

The first thing you’ll need to do to access the Graph API is to register your app in the Azure portal and provide access permission. As we’ve done previously, let’s grant the required permissions.

Graph-Api-Permissions

Using Microsoft Active Directory Authentication Library (ADAL), authenticate and grab the AccessToken.

Step 2: Call the Graph API

Once the AccessToken is retrieved, it’s easy to call the Graph API like any other secured API using HttpClient libraries. All of these APIs can be called from the endpoint https://graph.microsoft.com. As of now, there are two versions of the Graph API, one that’s ready to use in production (1.0) and another one currently in beta. You can call the particular version by appending it to the endpoint; for example, in this blog post I’m using the beta version by calling https://graph.microsoft.com/beta.

Calling the User API

Getting user details is super easy with the Graph APIs, including a great deal lot of information about the signed in user as well as other users. You can get more details about this API from here. The code to call the API is:

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", App.AuthenticationResult.AccessToken);
var response = await client.GetStringAsync("https://graph.microsoft.com/beta/users");
var result = JsonConvert.DeserializeObject<UsersRequest>(response);
UsersList.ItemsSource = result.Value;

Calling the Mail API

Just as we could easily query users, using the Graph API we can also query emails in Office 365:

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", App.AuthenticationResult.AccessToken);
var response = await client.GetStringAsync("https://graph.microsoft.com/beta/me/messages");
var result = JsonConvert.DeserializeObject<MailResponse>(response);
MailList.ItemsSource = result.Value;

Calling the Files API

The Files API gives access to a user’s OneDrive for Business account. Using this access, you can manipulate files and folders:

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", App.AuthenticationResult.AccessToken);
var response = await client.GetStringAsync("https://graph.microsoft.com/beta/me/drive/root/children");
var result = JsonConvert.DeserializeObject<FileResponse>(response);
FilesList.ItemsSource = result.Value;

Similarly, there is an API for the Calendar, Contacts, Groups and other directory objects that can all be called from one endpoint: https://graph.microsoft.com. Now you can build your own cross-platform Outlook!

Graph API Xamarin.Forms Client App

Wrapping Up

The new Graph API makes building clients for various Microsoft services easy and fun. Instead of having to use numerous different APIs and SDKs to access things like mail and files, we can access all of them easily with the Graph API. You can find more information about the Graph APIs here. For a look at the Graph APIs in action with Xamarin.Forms, download this sample from GitHub.