Skip to main content

Overview

The GraphServiceClient class is the main entry point for interacting with Microsoft Graph. It provides access to all Graph API resources through a fluent API and manages authentication, request execution, and resource initialization.

Constructor Overloads

Using TokenCredential

public GraphServiceClient(
    TokenCredential tokenCredential,
    IEnumerable<string> scopes = null,
    string baseUrl = null
)
tokenCredential
TokenCredential
required
Azure.Core TokenCredential for authenticating requests (e.g., ClientSecretCredential, DefaultAzureCredential)
scopes
IEnumerable<string>
List of permission scopes. Defaults to [".default"] if not specified
baseUrl
string
Base service URL. Defaults to https://graph.microsoft.com/v1.0

Using IRequestAdapter

public GraphServiceClient(
    IRequestAdapter requestAdapter,
    string baseUrl = null
)
requestAdapter
IRequestAdapter
required
Custom request adapter for making HTTP requests
baseUrl
string
Base service URL. Overrides the adapter’s base URL if provided

Using HttpClient and TokenCredential

public GraphServiceClient(
    HttpClient httpClient,
    TokenCredential tokenCredential,
    IEnumerable<string> scopes = null,
    string baseUrl = null
)
httpClient
HttpClient
required
Custom HttpClient instance with middleware pipeline
tokenCredential
TokenCredential
required
TokenCredential for authentication
scopes
IEnumerable<string>
Permission scopes for authentication
baseUrl
string
Base service URL

Using IAuthenticationProvider

public GraphServiceClient(
    IAuthenticationProvider authenticationProvider,
    string baseUrl = null
)
authenticationProvider
IAuthenticationProvider
required
Custom authentication provider implementing IAuthenticationProvider
baseUrl
string
Base service URL

Properties

RequestAdapter

public IRequestAdapter RequestAdapter { get; set; }
Access to the underlying request adapter for advanced scenarios.

Batch

public BatchRequestBuilder Batch { get; }
Request builder for creating batch requests to execute multiple operations in a single HTTP call.

Resource Properties

The client exposes all Graph API resources as properties:
  • Users - User management operations
  • Groups - Group management operations
  • Applications - Application registration management
  • ServicePrincipals - Service principal management
  • Drives - OneDrive and SharePoint drive operations
  • Sites - SharePoint site operations
  • Teams - Microsoft Teams operations
  • Chats - Chat operations
  • Me - Operations for the signed-in user
  • And many more…

Code Examples

Basic Initialization with ClientSecretCredential

using Azure.Identity;
using Microsoft.Graph;

var credential = new ClientSecretCredential(
    tenantId: "YOUR_TENANT_ID",
    clientId: "YOUR_CLIENT_ID",
    clientSecret: "YOUR_CLIENT_SECRET"
);

var graphClient = new GraphServiceClient(credential);

// Access resources
var users = await graphClient.Users.GetAsync();

Using DefaultAzureCredential

using Azure.Identity;
using Microsoft.Graph;

var credential = new DefaultAzureCredential();

var graphClient = new GraphServiceClient(
    credential,
    scopes: new[] { "https://graph.microsoft.com/.default" }
);

var me = await graphClient.Me.GetAsync();

Custom BaseUrl for Beta Endpoint

var graphClient = new GraphServiceClient(
    credential,
    baseUrl: "https://graph.microsoft.com/beta"
);

Using Custom HttpClient

var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(100);

var graphClient = new GraphServiceClient(
    httpClient,
    credential
);

Batch Requests

var batchRequestContent = new BatchRequestContent();

var userRequest = graphClient.Users["user-id"].ToGetRequestInformation();
var groupRequest = graphClient.Groups["group-id"].ToGetRequestInformation();

var userRequestId = await batchRequestContent.AddBatchRequestStepAsync(userRequest);
var groupRequestId = await batchRequestContent.AddBatchRequestStepAsync(groupRequest);

var batchResponse = await graphClient.Batch.PostAsync(batchRequestContent);

Disposal

public void Dispose()
The client implements IDisposable. Dispose when done to clean up resources:
using var graphClient = new GraphServiceClient(credential);
// Use client
// Automatically disposed

See Also

Authentication Providers

Learn about authentication options

Users API

User management operations

Groups API

Group management operations

Batch Requests

Execute multiple requests efficiently

Build docs developers (and LLMs) love