Query Microsoft Graph by using SDKs
The Microsoft Graph SDKs are designed to simplify building high-quality, efficient, and resilient applications that access Microsoft Graph. The SDKs include two components: a service library and a core library.
The service library contains models and request builders that are generated from Microsoft Graph metadata to provide a rich and discoverable experience.
The core library provides a set of features that enhance working with all the Microsoft Graph services. Embedded support for retry handling, secure redirects, transparent authentication, and payload compression, improve the quality of your application's interactions with Microsoft Graph, with no added complexity, while leaving you completely in control. The core library also provides support for common tasks such as paging through collections and creating batch requests.
In this unit, you learn about the available SDKs and see some code examples of some of the most common operations.
Note
The code samples in this unit are based on version 5.65 of the Microsoft Graph .NET SDK.
Install the Microsoft Graph .NET SDK
The Microsoft Graph .NET SDK is included in the following NuGet packages:
- Microsoft.Graph - Contains the models and request builders for accessing the
v1.0
endpoint with the fluent API.Microsoft.Graph
has a dependency onMicrosoft.Graph.Core
. - Microsoft.Graph.Beta - Contains the models and request builders for accessing the
beta
endpoint with the fluent API.Microsoft.Graph.Beta
has a dependency onMicrosoft.Graph.Core
. - Microsoft.Graph.Core - The core library for making calls to Microsoft Graph.
Create a Microsoft Graph client
The Microsoft Graph client is designed to make it simple to make calls to Microsoft Graph. You can use a single client instance for the lifetime of the application. The following code examples show how to create an instance of a Microsoft Graph client. The authentication provider handles acquiring access tokens for the application. The different application providers support different client scenarios. For details about which provider and options are appropriate for your scenario, see Choose an Authentication Provider.
var scopes = new[] { "User.Read" };
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "common";
// Value from app registration
var clientId = "YOUR_CLIENT_ID";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// Callback function that receives the user prompt
// Prompt contains the generated device code that you must
// enter during the auth process in the browser
Func<DeviceCodeInfo, CancellationToken, Task> callback = (code, cancellation) => {
Console.WriteLine(code.Message);
return Task.FromResult(0);
};
// /dotnet/api/azure.identity.devicecodecredential
var deviceCodeCredential = new DeviceCodeCredential(
callback, tenantId, clientId, options);
var graphClient = new GraphServiceClient(deviceCodeCredential, scopes);
Read information from Microsoft Graph
To read information from Microsoft Graph, you first need to create a request object, and then run the GET
method on the request.
// GET https://graph.microsoft.com/v1.0/me
var user = await graphClient.Me
.GetAsync();
Retrieve a list of entities
Retrieving a list of entities is similar to retrieving a single entity except there are other options for configuring the request. The $filter
query parameter can be used to reduce the result set to only those rows that match the provided condition. The $orderBy
query parameter requests that the server provides the list of entities sorted by the specified properties.
// GET https://graph.microsoft.com/v1.0/me/messages?
// $select=subject,sender&$filter=subject eq 'Hello world'
var messages = await graphClient.Me.Messages
.GetAsync(requestConfig =>
{
requestConfig.QueryParameters.Select =
["subject", "sender"];
requestConfig.QueryParameters.Filter =
"subject eq 'Hello world'";
});
Delete an entity
Delete requests are constructed in the same way as requests to retrieve an entity, but use a DELETE
request instead of a GET
.
// DELETE https://graph.microsoft.com/v1.0/me/messages/{message-id}
// messageId is a string containing the id property of the message
await graphClient.Me.Messages[messageId]
.DeleteAsync();
Create a new entity
For fluent style and template-based SDKs, new items can be added to collections with a POST
method.
// POST https://graph.microsoft.com/v1.0/me/calendars
var calendar = new Calendar
{
Name = "Volunteer",
};
var newCalendar = await graphClient.Me.Calendars
.PostAsync(calendar);