The world of Office 365 APIs is immense, with a plethora of capabilities that enable developers to access Office 365 and add rich functionality to their mobile apps, all from shared code. In fact, we’ve already covered several Office 365 topics, including integration of SharePoint Online, Outlook(Mail), and OneDrive, as well as authentication .
To consume these services, we followed several steps to register our native app in Azure AD, set proper permissions, authenticate, and receive an ‘access token’ using the Active Directory Authentication Library (or ADAL). Once an access token is obtained, it can be used for subsequent API calls against any of the Office 365 services listed above. Consuming a custom WebAPI secured by Azure AD is straightforward enough that you can call any of the Office 365 services by using HTTPClient and passing the ‘access token’.
There used to be a few more steps to consume an O365 API. For instance, before calling an Office 365 service API, developers were required to call Discovery Service, which “discovers” and provides different end-points for various O365 services. As you can imagine, this was a cumbersome and tedious task.
At Microsoft Build this year, Microsoft revealed the new Office 365 Unified API, which provides a single-unified-end-point to call any services hosted within O365.
In this post, we’ll see how we can use the O365 Unified API in a Xamarin.Forms app by reusing the sample we built in the blog post Put Some Azure Active Directory in Xamarin.Forms. You may want to revisit the blog post for reference.
Step 1: Give permission to access the Office 365 APIs
In the Azure Portal, we must set the required permissions so we can access the Office 365 services from our mobile apps. In this sample, we’ll cover the Me (the logged-in user’s details) and Files APIs. We covered these before in our post on OneDrive.
Step 2: Call Office 365 Services
In our sample from the previous blog post, when the login button was clicked, we asked the user to authenticate. Upon successful authentication, we displayed the user’s name in a message box. We’re going to extend this same method to create a new Tabbed Page with two tabs, one to display the user’s information and the other to list OneDrive documents.
Calling the “Me” API
The Me API is all about fetching details about a user who is signed in. We can use the following code to call this API.
private async void MePageAppearing(object sender, EventArgs e) { var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", App.AuthenticationResult.AccessToken); var meData = await client.GetStringAsync("https://graph.microsoft.com/beta/me"); var userData = JsonConvert.DeserializeObject<UserModel>(meData); //Set Inforation this.DisplayName.Text = userData.DisplayName; this.Mail.Text = userData.Mail; this.Country.Text = userData.Country; }
When we run the app, we should see the following output, which resembles the details stored on the Office 365 portal:
Calling the “Files” API
Previously, calling the Files API was part of adding the SharePoint permissions and accessing it using the discovery service. With the unified Files API, it has become much easier. The following code gives us all of the files in our OneDrive.
[code langauge=”csharp”] private async void FilesPageAppearing(object sender, EventArgs e) { var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", App.AuthenticationResult.AccessToken); var meData = await client.GetStringAsync("https://graph.microsoft.com/beta/me/files"); var data = JsonConvert.DeserializeObject<FilesModel>(meData); var files = from file in data.Value where file.Type.ToLower() == "file" select file.Name; this.FileList.ItemsSource = files.ToList(); }
[/code]
Again, if we run the app, we should be able to see a list of files from OneDrive for Business in the app:
Get Started Today
In this post, we’ve seen how easy it is to integrate Office 365 into Xamarin mobile apps using ADAL and the Office 365 Unified API. Using these APIs, it’s possible for anyone to build the next best mail, calendar, or meeting request app.
Get up and running today with the Office 365 Unified API by downloading this sample, adding your Client ID and Redirect URI in the HomePage.xaml.cs file, and registering the app in Azure AD with the proper permissions.
0 comments