MicrosoftGraph Service

The MicrosoftGraph Service aim to easily logon to Office 365 Service in order to:

  • Retrieve User Information
  • Retrieve and Send emails

Prerequisites

NOTE: This API will not work on an XBOX UWP Application

1. Get and Office 365 Subscription

If you don't have one, you need to create an Office 365 Developer Site. There are several ways to create one:

  • An MSDN subscription - This is available to MSDN subscribers with Visual Studio Ultimate and Visual Studio Premium.
  • An existing Office 365 subscription - You can use an existing Office 365 subscription, which can be any of the following: Office 365 Midsize Business, Office 365 Enterprise, Office 365 Education, Office 365 Government.
  • Free O365 trial - You can start with a free 30-day trial, or buy an Office 365 developer subscription.
  • Free O365 Developer - Or Get a One year free Office 365 Developer account

2. Register you application in Azure Active Directory

To authenticate your app, you need to register your app with Azure AD, and provide some details about your app. You can register your app manually by using the Azure Management Portal, or by using Visual Studio.

To register your app manually, see Manually register your app with Azure AD so it can access Office 365 APIs.

To register your app by using Visual Studio, see Using Visual Studio to register your app and add Office 365 APIs.

After you've registered your app, Azure AD will generate a client ID for your app. You'll need to use this client ID to get your access token.

When you register your app in the Azure Management Portal, you will need to configure details about your application with the following steps:

  1. Specify your application as a Web application and/or web API
  2. Specify the Redirect Uri as http://localhost:8000
  3. Add Application: Choose Microsoft Graph API
  4. Specify the permission levels the MicrosoftGraph Service requires from the Office 365 API (Microsoft Graph). Choose at least:
  5. Sign in and read user profile to access user's profile.
  6. Read user mail and Send mail as user to retrieve/send messages.

Note: Once register copy and save the Client ID for futur use.

Setting Value
Web application and/or web API Yes
Redirect Uri http://localhost:8080
Resource to Add Microsoft Graph
Delegate Permissions Sign in and read user profile, Read user mail and Send mail

Syntax

Sign in with an Office 365 account


// Initialize the service
if (!MicrosoftGraphService.Instance.Initialize(ClientId.Text))
{
 return;
}
// Login via Azure Active Directory 
if (!await MicrosoftGraphService.Instance.LoginAsync())
{
 return;
}

Get the connected user's info


// Retrieve user's info from Azure Active Directory
var user = await MicrosoftGraphService.Instance.User.GetProfileAsync();
UserPanel.DataContext = user;

// You can also select any fields you want in the response
MicrosoftGraphUserFields[] selectedFields = 
{
 MicrosoftGraphUserFields.Id,
 MicrosoftGraphUserFields.DisplayName,
 MicrosoftGraphUserFields.JobTitle,
 MicrosoftGraphUserFields.Mail,
 MicrosoftGraphUserFields.Department,
 MicrosoftGraphUserFields.PreferredLanguage
};

var user =await MicrosoftGraphService.Instance.User.GetProfileAsync(selectedFields);
UserPanel.DataContext = user;     

// Retrieve the user's photo 
using (IRandomAccessStream photoStream = await MicrosoftGraphService.Instance.User.GetPhotoAsync())
{
 BitmapImage photo = new BitmapImage();
 if (photoStream != null)
  {
   await photo.SetSourceAsync(photoStream);
  }
  else
  {
   photo.UriSource = new Uri("ms-appx:///SamplePages/MicrosoftGraph Service/user.png");
  }

  this.Photo.Source = photo;
}

Retrieve/Send messages

// Get the top 10 messages
messages = await MicrosoftGraphService.Instance.User.Message.GetEmailsAsync(10);
MessagesList.ItemsSource = messages;

// You can also select any fields you want in the response
MicrosoftGraphMessageProperties[] selectedFields = 
{ 
 MicrosoftGraphMessageProperties.Id,
 MicrosoftGraphMessageProperties.From,
 MicrosoftGraphMessageProperties.Subject,
 MicrosoftGraphMessageProperties.BodyPreview
};

messages = await MicrosoftGraphService.Instance.User.Message.GetEmailsAsync(10,selectedFields);
MessagesList.ItemsSource = messages;

// Request the next 10 messages 
messages = await MicrosoftGraphService.Instance.User.Message.NextPageEmailsAsync();
if (messages == null)
{
    // no more messages
}

// Send a message
string[] toRecipients = { "user1@contoso.com", "user2@contoso.com" };
string subject = "This is the subject of my message;
string content = "This is the content of my message";

await MicrosoftGraphService.Instance.User.Message.SendEmailAsync(subject, content, BodyType.Text, toRecipients);

// You can also send a message in html format
string content = GetHtmlMessage();
await MicrosoftGraphService.Instance.UseR.Message.SendEmailAsync(subject, content, BodyType.Html, toRecipients);


Example

MicrosoftGraph Service Sample Page

Requirements (Windows 10 Device Family)

Device family Universal, 10.0.10586.0 or higher
Namespace Microsoft.Toolkit.Uwp.Services

API

NuGet Packages Required

Microsoft.Toolkit.Uwp.Services See the NuGet Packages page for complete list.