Set up and run the Office 365 Android SDK Samples in Android Studio

How do you set up and run the samples in the Office 365 API Android SDK? This post provides a detailed step-by-step walkthrough. You can either read the post or watch the video, both of which cover the same content:

First, download the Office 365 SDK for Android from the GitHub repository. You can click on "Download Zip" on the page to download the SDK, or you can just press "Clone"

Open Android Studio, then go to File > Import Project. Look for the location you downloaded the SDK to, and go to the samples folder within that. Next, go into the folder "outlook" - I'm going to show you how to set up the "outlook" sample. Select build.gradle in this folder, and click OK.

At this point, Android Studio might prompt you "Language level changes will take effect on project reload. Would you like to reload project "outlook" now? Just click yes.

Your project structure should looks like this:

Press the Run button and run it on either your connected device or an emulator. You'll see that the app compiles and runs successfully, but it doesn't do anything.

What's wrong here? Well, we haven't yet registered this app with Azure Active Directory. Let's go do that now.

Go to Azure.com, and click on Portal at the top. Enter your login for Azure, and select your Active Directory that you want this app to be registered to. Let's say we want to test it on my clippy.onmicrosoft.com Office 365 tenant, so in this case, I select that tenant. You'll be able to select your own tenant here based on what you created previously.

Once you've select your Active Directory, click on "Applications" as shown below:

At the bottom, press "Add"

Select "Add an application my organization is developing" and on the next page, enter any name for your application. In this case, I'm using "Android-Outlook-Sample". Select "Native Client Application" since it's going to run on a mobile device.

On the next page, type anything you want into the Redirect URI box. I chose "https://www.google.com" as my redirect URI.

Press OK. Go to "Configure" and you'll see the following:

First we want to press the green "Add application" button below and select "Office 365 Exchange Online." Press OK, and select the following permission:

We just told Azure AD that we want this app to have some mail-specific permissions. So that means the app will be able to read and write a user's mail, and send mail as the user. Don't forget to click on "Save" at the bottom of the screen to save this change!

Now, go back to Android Studio and go to app > java > com.microsoft.services > samples.outlook > ServiceConstants.java in the project navigator view. Fill in the REDIRECT_URL and CLIENT_ID fields with the values in Azure AD (if you're not sure where to get these values, refer to the second-last image before this paragraph, that shows you where the redirect URL and client ID are). This is what you should have:

Screen Shot 2015-01-08 at 10.00.37 PM.png

Now, run the app again. Immediately you see that you are presented with a login screen. Sign in to Office 365 with your credentials.

Click on "Play with Email"

You don't see anything yet. Tap on the three dots at the top right to bring up the menu, which contains of only one item, Settings. Tap on that and enter your email address. It should be the email address of the same user that you previously logged in to:

Press Enter, and then press "Update". If your mail doesn't show up, you might want to just tap on the Back Button to go back to the screen with the Mail & Calendar buttons, and tap on Mail again, and then hit update. It might take a while.

You should see your emails in your inbox show up!

Of course, you can also use the Calendar functionality in this sample, but you'll want to go into Azure AD first and add the relevant Calendar permissions. If you've any questions, post them in the Comments below.

Office 365 iOS SDK Swift Sample -- Create, Read, Update and Delete Files

Here are some code snippets to show you how to create, read, update and delete files (i.e. the CRUD operations) from OneDrive for Business/SharePoint using the Office 365 SDK for iOS. The underlying implementation is a REST-based API, but we don't have to worry about this here - we can just use the abstract objects created for us in the SDK.

For a detailed step-by-step explanation of how to set up a project that uses the Office 365 Objective-C SDK in Swift, check out this other article I wrote.

Before doing anything: Create an MSSharePointClient

The parameter for url is your SharePoint URL. To figure that out, go to portal.office.com and click on OneDrive. Copy the front part of that address on the page and put /_api/v1.0/me after it. You can also use Discovery Service to figure out what your OneDrive for Business API endpoint is.

In order to get the access token, you either use the Azure Active Directory Authentication Library (ADAL) [see how to use it here] or you can hard-code one in (NOT recommended; use only for debug & prototyping purposes).

Get Files: Let's get all the files in our root folder.

Line 6 is assuming you are populating a table with the data - you'll always want to reload the table. (Scroll to the end of this post for details on the sample I created, that uses a table) 

To iterate over the files and get their properties, you can do something like:

Use intellisense to figure out what are the properties a file has.

Get Content of a Single File: Let's say you essentially want to download a file and read its contents. How do you do that? See the code sample below:

Line 4 prints out the byte array, while Line 5 converts it into a string first before printing it.

Create File: The following code snippets show how to create a file. There are two calls you have to make, addItem and putContent.

Update File: Now, let's learn how to update an already existing file. This is pretty much a call to putContent, the same as the putContent part of creating a file.

Delete File: Finally, let's see how we can delete a file. 

Line 1 of the code snippet above contains a tableview specific property, indexPath.row. Also, please note that you need to add a custom header with the name "If-Match" as shown above. As quoted from the official MSDN documentation library,

The update and delete APIs require If-Match HTTP header to be provided with eTag of the resource in order to confirm that the client is modifying the resource from previously known state. A generic value of * could be provided to override the check. However it is not advised to override this option.

Since we are doing all this in context of a table, let's see what the full sample app looks like in Swift.

Note: This is a super bare-bones app that I made within an hour, so I'm not going to try to pretend that it contains the best UI or Swift coding practices. But it does get the job done -- it shows you how to use the Office 365 iOS SDK in Swift.

Get the sample code here from my GitHub repository.

Here's what the FilesTableViewController looks like:

When the app is run, I am first prompted to sign in. Note: This sign in screen only shows up the first time.

After I do so, I click on "Files". This brings me to a table with all my files listed:

Pull to refresh works in this view, so drag down and release to refresh and get the files from the server again. The corresponding method in the FilesViewController is the getFiles method.

Tapping on "+" brings up an AddFileController, and allows you to type in a title for the file and edit its contents (by default, it's some Lorem Ipsum text). Tap upload to upload the file as shown in the screenshot below:

Back at the screen that has the list of all files, tap on a row in the table to view more details of the file. This is the "Read" part of the CRUD operations to read the file contents & properties. As the image below shows, you can see the datetime last modified, as well as the title of the file in the navigation bar.

You'll also see the contents of the file in an editable text view, that you can tap on to edit. After editing, press Update File to save the changes to the server.

Sliding left on a cell shows a Delete button, and it deletes the actual file from the OneDrive for Business cloud storage.

Check out the methods in FilesViewController to see all these in action. Once again, here's the link to the GitHub repo with the full working sample. I hope this quick guide has been useful to you - leave your questions in the Comments section below.

Using the Office 365 iOS SDK in Swift

In June 2014, Apple released what I consider to be one of its best products ever - a new language, Swift. Swift is fast, concise and extremely fun to write. But what about the hundreds of thousands of libraries out there written in Objective-C, including the Office 365 SDK for iOS?

In this article, I'll show you how to set up the Office 365 SDK for iOS in Swift. By the end of this guide, you will have successfully set up a new Swift project that connects to Office 365 and lists all your OneDrive for Business files in a table. Here's where you can find the accompanying code sample.

There is a guide written by Richard diZerega titled Developing iOS Apps with Azure and Office 365 APIs. It's a very detailed and great piece, but the key difference between his guide and mine is that mine uses the objects in the Office 365 iOS SDK to interact with the API, while his takes the approach of constructing calls using NSMutableURLRequest. I highly prefer to and recommend that you always use the SDK's abstractions when they're provided, and I hope that this guide will allow you to see how to do so with Swift.

Create a new project in Xcode. Let's call ours O365Demo. Then, go to the terminal and create a podfile. If this doesn't work for you, you probably don't have Cocoapods installed. This is the standard dependency-management tool for iOS development; head on over here to learn how to set it up.

Next, open the podfile you just created with Xcode. To put this in code, here are the three commands you have to type into the terminal:

Next, add two lines of code into your podfile to specify that you'll be using the Active Directory Authentication Library (ADAL) by Azure, and the Office 365 iOS SDK. Your podfile should look like this when you're done:

Next step is to install the pods, then open the workspace (close the project it's still open in Xcode). Go to the terminal and type this:

Add libPods-ADALiOS.a under Linked Frameworks and Libraries as shown in the screenshot below:

Don't worry that libPods-ADALiOS.a is in red. You can ignore the red highlighting for now - it will work.

Create a new Objective C file in your O365Demo project. Name it anything. You will get prompted to add a Bridging Header file:

After doing so, delete the Objective-C file that was created but leave the bridging header alone. (i.e. delete the highlighted file in the screenshot below)

Add these import statements to the bridging header file:

Now, let's go set up our actual app. We want to create a sample app with two screens - the first screen will ask you to login to Office 365, and after you login, you can press the "Get Files" button. This will then bring you to the second screen, which will show you a list of all your files in your root folder in OneDrive for Business (i.e. SharePoint).

Create a Table View Controller in Main.storyboard, and create a Button (with the label "Get Files") in the original View Controller. Control click on the button, and drag-and-drop to the table view controller to create a "Show" segue to the Table View Controller. Your storyboard should look like this:

This shows the table view, when the "Get Files" button is tapped.

Next, create a new Swift file and name it "FilesTableViewController". This will be our subclass of TableViewController. Replace any existing code in your FilesTableViewController.swift file with the following:

Make sure that the table view controller in your storyboard is the same one that you just subclassed. To do so, click on the table view controller in the Storyboard file and change its class to "FilesTableViewController" in the Inspector.

Notice I created an array which holds objects of type MSSharePointItem. This array is going to hold information about my files from SharePoint. Till now, I haven't made any API calls to the Office 365 server to populate it, so it'll remain empty. Run it and you will see that the app runs successfully - but nothing shows up in the table yet.

Now, let's actually add code to authenticate to Active Directory and get the Office 365 files. 

Go to ViewController.swift. We're going to add code that automatically signs the user in. Replace the contents of ViewController.swift with the following:

Note that I'm able to make a call to the Office 365 Objective-C library with the bridging header that we created earlier. This allowed me to write a bunch of method calls to the library in Swift. If you're not sure what the method signature of an Objective-C method is in Swift, use intellisense/autocomplete to help you.

Before you run this, though, you have to create an app in Azure AD. Read this article that I recently published for a step-by-step on how to do this (you'll want to start from Step 6 in that article). Once you have the app created, replace the variables in the code above with ones specific to your tenant and app in Azure AD.

Now, go back to FilesTableViewController.swift. Let's add code here to actually make an API request to the Office 365 Files API endpoint and return a list of our files. As I said earlier, what's great about using an SDK is that you don't have to construct HTTP requests by yourself; instead you can use the objects that the SDK provides to abstract this underlying implementation away.

Add the following chunk of code to after self.tableview.registerClass in the viewDidLoad method of FilesTableViewController.swift:

Run the app and you'll get a sign-in screen:

After you sign in, click "Get Files", and a list of your files will show.

That's it! Once again, if you want to download the project, it's on GitHub. If you have any questions, leave them in the Comments below.