Microsoft Graph

Unified API model to access data in M365.

3 components

  • Graph API ( https://graph.microsoft.com/)
  • Graph connectors (deliver data outside of Microsoft cloud into Graph)
  • Graph Data Connect (deliver Graph data to Azure data stores)

Primary namespace: microsoft.graph

Call a REST API Method

Syntax: {HTTP method} https://graph.microsoft.com/{version}/{resource}?{query-parameters}
Example: GET https://graph.microsoft.com/%7bversion%7d/%7bresource%7d?%7bquery-parameters%7d

HTTP Methods

  • GET, POST, PATCH, PUT, DELETE.
  • GET and DELETE do not require a request body.
  • POST, PATCH, PUT usually require a request body in JSON.

Microsoft Graph .NET SDK

Packages

  • Microsoft.Graph – service library that contains models and request builders.
  • Microsoft.Graph.Core – core library for making calls to Microsoft Graph.

Code

Create a Graph Client

Use a single client for the lifetime of the application.

// Build a client application.
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
    .Create("INSERT-CLIENT-APP-ID")
    .Build();

// Create an authentication provider by passing in a client application and graph scopes.
DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, graphScopes);

// Create a new instance of GraphServiceClient with the authentication provider.
GraphServiceClient graphClient = new GraphServiceClient(authProvider);

Read Information

// GET https://graph.microsoft.com/v1.0/me

var user = await graphClient.Me
    .Request()
    .GetAsync();

Retrieve a List of Entities

// GET https://graph.microsoft.com/v1.0/me/messages?$select=subject,sender&$filter=<somecondition>&orderBy=receivedDateTime

var messages = await graphClient.Me.Messages
    .Request()
    .Select(m => new {
        m.Subject,
        m.Sender
    })
    .Filter("<filter condition>")
    .OrderBy("receivedDateTime")
    .GetAsync();

Delete an Entity

// DELETE https://graph.microsoft.com/v1.0/me/messages/{message-id} (https://graph.microsoft.com/v1.0/me/messages/%7bmessage-id%7d)

string messageId = "AQMkAGUy...";
var message = await graphClient.Me.Messages[messageId]
    .Request()
    .DeleteAsync();

Create a New Entity

// POST https://graph.microsoft.com/v1.0/me/calendars

var calendar = new Calendar
{
    Name = "Volunteer"
};

var newCalendar = await graphClient.Me.Calendars
    .Request()
    .AddAsync(calendar);

Documentation