Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Microsoft publishes ready-to-use SDKs for accessing Microsoft Graph APIs in many popular programming languages. These SDKs are a convenient way to access Microsoft Graph, just install the SDK with the appropriate package manager, and start coding. However, due to the size and scope of the Microsoft Graph REST API, these libraries are fairly large. For applications where overall installation size is a concern, this large size raises a concern, especially if the application only uses a subset of Microsoft Graph. In this case, generating a custom API client with Kiota that targets only the parts of Microsoft Graph used by your application can reduce the overall installation size of your app.
Using a Kiota-generated client has some advantages and disadvantages over using the Microsoft-published SDKs.
To generate a Microsoft Graph client with Kiota, you must provide an OpenAPI description for Microsoft Graph. You can find OpenAPI descriptions for Microsoft Graph here:
https://aka.ms/graph/v1.0/openapi.yaml
https://aka.ms/graph/beta/openapi.yaml
In order to reduce the overall size of the generated client, you need to identify the Microsoft Graph APIs used by your app, and use the --include-path
or --exclude-path
parameters to the Kiota generate command to only generate the models and request builders for those APIs.
For example, consider an application that uses the To Do APIs to manage a user's To Do tasks and task lists. All of the required APIs, such as List task lists and Create task, are accessed via URLs under the /me/todo
request URL.
GET /me/todo/lists
POST /me/todo/lists/{listId}/tasks
In this case, you can use the --include-path
parameter to only generate models and request builders for APIs under the /me/todo
path:
kiota generate --openapi https://aka.ms/graph/v1.0/openapi.yaml --include-path /me/todo/** ...
Let's explore an example of generating a client for C# to call the To Do APIs. Using the Microsoft Graph .NET client library, the app could get the signed-in user's task lists with the following code.
using Azure.Identity;
using Microsoft.Graph;
var credential = new DeviceCodeCredential();
var graphClient = new GraphServiceClient(credential);
var taskLists = await graphClient.Me.Todo.Lists.GetAsync();
Alternatively, the app could use a Kiota-generated library to call the same API. For a client generated with the following command:
kiota generate --openapi https://aka.ms/graph/v1.0/openapi.yaml --include-path /me/todo/** --language CSharp --class-name TaskClient --namespace-name MyTaskApp.Client --output ./src/MyTaskApp/Client
The equivalent code looks like:
using Azure.Identity;
using Microsoft.Kiota.Authentication.Azure;
using Microsoft.Kiota.Http.HttpClientLibrary;
using MyTaskApp.Client;
// The auth provider will only authorize requests to
// the allowed hosts, in this case Microsoft Graph
var allowedHosts = new [] { "graph.microsoft.com" };
var graphScopes = new [] { "User.Read" };
var credential = new DeviceCodeCredential();
var authProvider = new AzureIdentityAuthenticationProvider(credential, allowedHosts, scopes: graphScopes);
var adapter = new HttpClientRequestAdapter(authProvider);
var taskClient = new TaskClient(adapter);
var taskLists = await tasksClient.Me.Todo.Lists.GetAsync();
Getting the user's task lists could return a paged response. The Microsoft Graph core libraries provide a page iterator class developers can use to page through the collection of task lists.
For example, in C#, add a dependency to Microsoft.Graph.Core (dotnet add package Microsoft.Graph.Core
), then use the PageIterator
class to page through the collection.
using MyTaskApp.Client;
using MyTaskApp.Client.Models;
using Microsoft.Graph;
// Using adapter and taskLists from previous example
var pageIterator = PageIterator<TodoTaskList, TodoTaskListCollectionResponse>
.CreatePageIterator(
adapter,
taskLists,
(taskList) =>
{
Console.WriteLine(taskList.DisplayName);
return true;
});
await pageIterator.IterateAsync();
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Learning path
Explore Microsoft Graph scenarios for ASP.NET Core development - Training
This learning path includes hands-on exercises that will show you how to perform common tasks, such as showing a user's emails, accessing calendar events, and downloading and uploading files, in an ASP.NET Core app using Microsoft Graph APIs.