Skip to content

lebaker/O365-Android-Snippets

 
 

Repository files navigation

Office 365 Code Snippets for Android

Table of contents

This sample for Android is a repository of simple method examples that access email, calendar events, contacts, and files in Office 365. These "snippet" methods are self contained so you can paste them into your own code or use as reference for learning. This sample has snippets for the most common create, read, update, and delete operations on Office 365 content.

This sample includes the following operations:

Calendar

  • Get existing calendar events by date range.
  • Create an event.
  • Update an event.
  • Delete an event.
  • Accept an event invitation.
  • Decline an event invitation.
  • Get the status of an event attendee.

OneDrive for Business

  • Get existing files and folders.
  • Create text files.
  • Delete files or folders.
  • Read text file contents.
  • Update text file contents.
  • Upload or download files.

Mail

  • Get existing mail items.
  • Get mail items by subject.
  • Delete mail item.
  • Send new mail.
  • Forward email.
  • Reply to email.
  • Send email with text file attachment
  • Get email attachments from inbox

Contacts

  • Get existing contacts.
  • Create a contact.
  • Update a contact.
  • Delete a contact.

We will continue adding snippets to this sample, so make sure to check back.

Change History

April 6, 2015:

  • Initial release

April 16, 2015:

  • Added snippets for sending email with text file attachments and getting attachments from email in your inbox

Device requirements

To run the snippets project your device needs to meet the following requirements:

  • Android API level 16 or newer.

Prerequisites

To use the Office 365 Code Snippets for Android you need the following:

  • The latest version of Android Studio.
  • The Gradle build automation system version 2.2.1 or later.
  • An Office 365 account. You can sign up for an Office 365 Developer subscription that includes the resources that you need to start building Office 365 apps.
  • Java Development Kit (JDK) 7.
  • A registered Azure application with a client id and redirect uri value. The application must have the following permissions:
    • Windows Azure Active Directory
      • Enable sign-on and read user's profiles.
    • Office 365 SharePoint Online
      • Edit or delete users' files.
    • Office 365 Exchange Online
      • Read and write access to users' mail.
      • Send mail as a user.
      • Get full access to users' calendars.
      • Get full access to users' contacts.

Open the project using Android Studio

Open the project by importing the settings.gradle file.

  1. Register a new app in Azure Active Directory

Note: Be sure to select the native client application on step 5 of the Azure Management Portal azure application procedure.

  1. Install Android Studio and add the Android SDK packages according to the instructions on developer.android.com.

  2. Download or clone the Office 365 Code Snippets for Android.

  3. Start Android Studio.

  4. From the Welcome to Android Studio dialog box, choose Import project (Eclipse ADT, Gradle, etc).

  5. Select the settings.gradle file in the O365-Android-Snippets folder and click OK.

  6. Open the constants.java file in the com.microsoft.office365.snippetapp.helpers package.

  7. Find the CLIENT_ID constant and set its String value equal to the client id you registered in Azure Active Directory.

  8. Find the REDIRECT_URI constant and set its String value equal to the redirect URI you registered in Azure Active Directory. Office 365 Snippet sample

  9. Grant proper permissions to the sample application in Azure.

Note: The starter project declares the required dependencies using Gradle. The dependencies are:

Run the project

Once you've built the snippets project you can run it on an emulator or device.

  1. Run the project.
  2. Click the "Connect" item on the action bar overflow menu and enter your credentials.
  3. Click a task in the main activity list to run a task.
  4. View the results of the task in the detail pane to the right of the list.
  5. Optionally, clear the results of the task by clicking the "Clear Results" action bar item.
  6. Run all tasks in the list sequentially by clicking the "Run All" action bar item.

Understand the code

The starter project uses these classes to manage interactions with Office 365:

These classes have snippet methods that wrap calls to the SharePointClient and OutlookClient objects in the Office 365 SDK for Android. Look at the classes in the com.microsoft.office365.snippetapp.O365Stories package to see how the snippets are called.

Connect to Office 365

The Office 365 SDK for Android uses the Azure Active Directory Library (ADAL) for Android for connecting your app to Office 365. The ADAL provides protocol support for OAuth2, Web API integration with user level consent, and two-factor authentication.

The AuthenticationController object manages getting a token from ADAL and returning it to your application.

Calendar API

The CalendarSnippets object wraps the API operations that create, update and delete calendar events in an Office 365 Exchange calendar.

The getO365Events method gets a list of events from the Office 365 calendar and loads pages of events into a local list. Changes, deletions, and additions to this list are posted asynchronously to the Office 365 calendar by the updateCalendarEvent, deleteCalendarEvent, and createCalendarEvent methods.

Files API

The FileFolderSnippets object wraps the API operations that create, update, and delete files stored on OneDrive for Business.

The getFilesAndFoldersFromService method gets a list of all of the files and folders that are stored on OneDrive for Business and loads the list into a local array. Changes, deletions, and additions to the local list of files are posted asynchronously to OneDrive for Business by the postUpdatedFileToServer, deleteFileFromServer, and postNewFileToServer methods.

The getFilesAndFolders method returns a list of Item objects containing the selected files contents.

Mail API

The EmailSnippets object wraps the API operations that create, update and delete mail items in an Office 365 Exchange mailbox.

The getMailMessages method gets a list of mail items from the Office 365 mailbox and loads pages of items into a local list. Deletions to this list, and mail sent from the mailbox are posted asynchronously to the Office 365 calendar by the deleteMail, sendMail, forwardMail, and replyToEmailMessage methods.

Contacts API

The ContactsSnippets object wraps the API operations that create, update and delete contacts in Office 365 Exchange.

The getContacts method gets a list of contacts from Office 365 and loads pages of contacts into a local list. Changes, deletions, and additions to this list are posted asynchronously to Office 365 by the updateContact, deleteContact, and createContact methods.

Questions and comments

We'd love to get your feedback on this Android starter kit. You can send your questions and suggestions to us:

## Additional resources

Copyright

Copyright (c) 2015 Microsoft. All rights reserved. Microsoft and OneDrive are either registered trademarks or trademarks of Microsoft Corporation.

About

A repository of code snippets that wrap the Microsoft Office 365 SDK for Android

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Java 100.0%