Integrate the Microsoft Graph API into your .NET project!
The Microsoft Graph .NET Core Client Library contains core classes and interfaces used by Microsoft.Graph Client Library to send native HTTP requests to Microsoft Graph API. The core client library targets .NetStandard 1.1 and .Net Framework 4.5.
To install the client library via NuGet:
- Search for
Microsoft.Graph.Core
in the NuGet Library, or - Type
Install-Package Microsoft.Graph.Core
into the Package Manager Console.
Register your application to use Microsoft Graph API by following the steps at Register your application with the Microsoft identity platform.
The Microsoft Graph .NET Client Library does not currently include any default authentication implementations.
There are set of preview authentication providers available in the msgraph-sdk-dotnet-auth repo. Alternatively, you can use the built-in DelegateAuthenticationProvider class to authenticate each request.
For more information on DelegateAuthenticationProvider
, see the library overview.
The recommended library for authenticating against Microsoft Identity (Azure AD) is MSAL.
For an example of authenticating a UWP app using the V2 Authentication Endpoint, see the Microsoft Graph UWP Connect Library.
You can create an instance of HttpClient that is pre-configured for making requests to Microsoft Graph APIs using GraphClientFactory
. To create an instance of .NET's HttpClient
, you need to provide an instance of
IAuthenticationProvider
which can authenticate requests to Microsoft Graph.
HttpClient httpClient = GraphClientFactory.Create(IAuthenticationProvider, version: "beta");
For more information on initializing a client instance, see the library overview
Once you have an authenticated HttpClient
, you can begin to make calls to the service. The requests to the service follows our REST API syntax.
For example, to retrieve a user's default drive:
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "me/drive");
HttpResponseMessage response = await httpClient.SendAsync(requestMessage);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
}
To get the current user's root folder of their default drive:
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "me/drive/root");
HttpResponseMessage response = await httpClient.SendAsync(requestMessage);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
}
Install System.Runtime.InteropServices.RuntimeInformation before you install Microsoft.Graph >=1.3 if you are having an issue updating the package for a Xamarin solution. You may need to updated references to Microsoft.NETCore.UniversalWindowsPlatform to >=5.2.2 as well.
To view or log issues, see issues.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
- NuGet Package: https://www.nuget.org/packages/Microsoft.Graph.Core
If you are looking to build the library locally for the purposes of contributing code or running tests, you will need to:
- Have the .NET Core SDK (> 1.0) installed
- Run
dotnet restore
from the command line in your package directory - Run
nuget restore
andmsbuild
from CLI or run Build from Visual Studio to restore Nuget packages and build the project
Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license. See Third Party Notices for information on the packages referenced via NuGet.