in Office 365, Office 365 API, Visual Studio, VS2013

Getting Started with Office 365 APIs and Xamarin Projects

If you are looking for cross-platform development within Visual Studio, Xamarin is a great option to build applications not just for Windows, also for iOS and Android devices in C#, the language we all love.

If you are looking to integrate Office 365 APIs in your Xamarin Android and iOS project, then this Getting Started guide is for you!

ADAL for Xamarin

One of the key things to understand before jumping is about the availability of Azure Active Directory Authentication Library (ADAL) for Xamarin iOS and Android projects. Today, the supported way to use ADAL in an Xamarin project is to build a Xamarin binding to ADAL’s native Android Library. The generated binding can then be referenced by specific Android projects to authenticate and get tokens for Office 365 API resources.

ADAL team is planning on adding more robust support to Xamarin projects, but until then, Xamarin bindings will help you build the ADAL library you need for your Android projects. You can download and use ADAL v3.0 preview if you are interested to see ADAL vNext. ADAl v3.0 is a PCL and comes with support for Xamarin Android and iOS projects as well. However, please remember that ADAL v3.0 is still a preview and is not recommended to use in production. Here is an ADAL sample that uses ADAL v3.0 preview.

Below are the steps to build the Xamarin Bindings project and reference it in your Xamarin Android project.

Using the Office 365 API Tools

When you use Office 365 API Tools with Xamarin projects, tools adds a bunch of Office 365 API client libraries depending on the services you chose.

However, remove the following from your project every time you configure the APIs using the tool.

Another option is to not use tools for the time being while we are working on a fix already! Until then, You can manually register and manage your applications in Windows Azure Portal.

Compiling ADAL Native Android Library

1. Download the Java ADAL source code here

2. Build the libraries required to compile

Open PowerShell
cd azure-activedirectory-library-for-android/libs
.\getLibs.ps1
Copy android-support-v4.jar from android-sdk\extras\android\support\v4 to azure-activedirectory-library-for-android/libs

3. Compile the project to generate the .jar package

cd azure-activedirectory-library-for-android/src
android update project -p .
ant debug

4. Zip the generate layout and other resources required for the authentication UI

zip -r AdalWithResources.zip bin/classes.jar bin/AndroidManifest.xml res

If you are in windows (where zip command is not available), just make sure you are zipping the following files to AdalWithResources.zip:

  • bin/classes.jar
  • bin/AndroidManifest.xml
  • res (the whole folder)

Building the Xamarin Bindings Project

1. Open Visual Studio

2. Create a new Xamarin Bindings project

File->New->Android->Bindings Library (Android)

3. Add the following files under Jars folder and set their Build Action as follows:

AdalWithResources.zip, Build Action: LibraryProjectZip
azure-activedirectory-library-for-android-master\src\bin\classes.jar, Build Action: EmbeddedJar
azure-activedirectory-library-for-android-master\src\libs\gson.jar, Build Action: EmbeddedReferenceJar
azure-activedirectory-library-for-android-master\src\libs\android-support-v4.jar, Build Action: EmbeddedReferenceJar

Here is a screenshot of my binding project Jars folder:

xamarin-bindings-jars

4. Open the Metadata.xml under Transforms folder and replace the whole file with the following:

<metadata>
 <!--
 This sample removes the class: android.support.v4.content.AsyncTaskLoader.LoadTask:
 <remove-node path="/api/package[@name='android.support.v4.content']/class[@name='AsyncTaskLoader.LoadTask']" />
 
 This sample removes the method: android.support.v4.content.CursorLoader.loadInBackground:
 <remove-node path="/api/package[@name='android.support.v4.content']/class[@name='CursorLoader']/method[@name='loadInBackground']" />
 -->
 <!-- com.microsoft.aad.adal -->
 <attr path="/api/package[@name='com.microsoft.aad.adal']/class[@name='AuthenticationRequest']" name="visibility">public</attr>
 <remove-node path="/api/package[@name='com.microsoft.aad.adal']/class[@name='UserInfo']/constructor[@name='UserInfo' and count(parameter)=1 and parameter[1][@type='com.microsoft.aad.adal.IdToken']]" />
</metadata>

5. Build the project and it should generate the binding

Write Code to Authenticate and Interact with Office 365 API

Now that we have the bindings generated, we can reference the generated ADAL binding in the Android project and start writing ADAL code to authenticate and get tokens from Office 365 API services.

You can have a look at the sample – Xamarin Android Office 365 API Sample – published here at Office GitHub.

Follow the README to successfully compile and debug the project.

Once you have all set up, the code looks like this, exactly the same as you write in your .NET projects:

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.

Webmentions

  • SharePoint Mobile – Learning Resources | Todd Baginski's Blog

    […] Getting Started with O365 APIs in Xamarin Projects […]