The Microsoft Graph SDK service libraries provide a client class that you can use as the starting point for creating all API requests. There are two styles of client class: one uses a fluent interface to create the request (for example, client.Users["user-id"].Manager) and the other accepts a path string (for example, api("/users/user-id/manager")). When you have a request object, you can specify a variety of options such as filtering and sorting, and finally, you select the type of operation you want to perform.
There is also the Microsoft Graph PowerShell SDK, which has no client class at all. Instead, all requests are represented as PowerShell commands. For example, to get a user's manager, the command is Get-MgUserManager. For more information on finding commands for API calls, see Navigating the Microsoft Graph PowerShell SDK.
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.
The Microsoft Graph SDK for Go is currently in preview. Use of this SDK in production is not supported.
// GET https://graph.microsoft.com/v1.0/me
result, err := client.Me().Get(nil)
Use $select to control the properties returned
When retrieving an entity, not all properties are automatically retrieved; sometimes they need to be explicitly selected. Also, in some scenarios it isn't necessary to return the default set of properties. Selecting just the required properties can improve the performance of the request. You can customize the request to include the $select query parameter with a list of properties.
// GET https://graph.microsoft.com/v1.0/me?$select=displayName,jobTitle
var user = await graphClient.Me
.Request()
.Select(u => new {
u.DisplayName,
u.JobTitle
})
.GetAsync();
// GET https://graph.microsoft.com/v1.0/me?$select=displayName,jobTitle
let user = await client.api('/me')
.select('displayName', 'jobTitle')
.get();
// GET https://graph.microsoft.com/v1.0/me?$select=displayName,jobTitle
final User user = graphClient.me()
.buildRequest()
.select("DisplayName,JobTitle")
.get();
# GET https://graph.microsoft.com/v1.0/users/{user-id}?$select=displayName,jobTitle
$userId = "71766077-aacc-470a-be5e-ba47db3b2e88"
# The -Property parameter causes a $select parameter to be included in the request
$user = Get-MgUser -UserId $userId -Property DisplayName,JobTitle
Important
The Microsoft Graph SDK for Go is currently in preview. Use of this SDK in production is not supported.
Retrieving a list of entities is similar to retrieving a single entity except there a number of 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 will request that the server provide the list of entities sorted by the specified properties.
Note
Some requests for Azure Active Directory resources require the use of advanced query capabilities. If you get a response indicating a bad request, unsupported query, or a response that includes unexpected results, including the $count query parameter and ConsistencyLevel header may allow the request to succeed. For details and examples, see Advanced query capabilities on Azure AD directory objects.
The object returned when retrieving a list of entities is likely to be a paged collection. For details about how to get the complete list of entities, see paging through a collection.
Access an item of a collection
For SDKs that support a fluent style, collections of entities can be accessed using an array index. For template-based SDKs, it is sufficient to embed the item identifier in the path segment following the collection. For PowerShell, identifiers are passed as parameters.
// GET https://graph.microsoft.com/v1.0/me/messages/{message-id}
string messageId = "AQMkAGUy..";
var message = await graphClient.Me.Messages[messageId]
.Request()
.GetAsync();
// GET https://graph.microsoft.com/v1.0/me/messages/{message-id}
let messageId = 'AQMkAGUy..';
let messages = await client.api(`/me/messages/${messageId}`)
.get();
// GET https://graph.microsoft.com/v1.0/me/messages/{message-id}
final String messageId = "AQMkAGUy..";
final Message message = graphClient.me().messages(messageId)
.buildRequest()
.get();
// GET https://graph.microsoft.com/v1.0/me/messages/{message-id}?$expand=attachments
string messageId = "AQMkAGUy...";
var message = await graphClient.Me.Messages[messageId]
.Request()
.Expand("attachments")
.GetAsync();
// GET https://graph.microsoft.com/v1.0/me/messages?$expand=attachments
let messageId = 'AQMkAGUy..';
let message = await client.api(`/me/messages/${messageId}`)
.expand('attachments')
.get();
// GET https://graph.microsoft.com/v1.0/me/messages/{message-id}?$expand=attachments
final String messageId = "AQMkAGUy...";
final Message message = graphClient.me().messages(messageId)
.buildRequest()
.expand("attachments")
.get();
# GET https://graph.microsoft.com/v1.0/users/{user-id}/messages?$expand=attachments
$userId = "71766077-aacc-470a-be5e-ba47db3b2e88"
$messageId = "AQMkAGUy.."
# -ExpandProperty is equivalent to $expand
$message = Get-MgUserMessage -UserId $userId -MessageId $messageId -ExpandProperty Attachments
Important
The Microsoft Graph SDK for Go is currently in preview. Use of this SDK in production is not supported.
For SDKs that support a fluent style, new items can be added to collections with an Add method. For template-based SDKs, the request object exposes a post method. For PowerShell, a New-* command is available that accepts parameters that map to the entity to add. The created entity is usually returned from the call.
// POST https://graph.microsoft.com/v1.0/me/calendars
var calendar = new Calendar
{
Name = "Volunteer"
};
var newCalendar = await graphClient.Me.Calendars
.Request()
.AddAsync(calendar);
// POST https://graph.microsoft.com/v1.0/me/calendars
final Calendar calendar = new Calendar();
calendar.Name = "Volunteer";
final Calendar newCalendar = graphClient.me().calendars()
.buildRequest()
.post(calendar);
The Microsoft Graph SDK for Go is currently in preview. Use of this SDK in production is not supported.
// POST https://graph.microsoft.com/v1.0/me/calendars
import (
calendars "github.com/microsoftgraph/msgraph-sdk-go/me/calendars"
graph "github.com/microsoftgraph/msgraph-sdk-go/models/microsoft/graph"
)
calendar := graph.NewCalendar()
name := "Volunteer"
calendar.SetName(&name)
options := calendars.CalendarsRequestBuilderPostOptions{
Body: calendar,
}
result, err := client.Me().Calendars().Post(&options)
Updating an existing entity with PATCH
Most updates in Microsoft Graph are performed using a PATCH method and therefore it is only necessary to include the properties that you want to change in the object you pass.
// PATCH https://graph.microsoft.com/v1.0/teams/{team-id}
var team = new Team
{
FunSettings = new TeamFunSettings
{
AllowGiphy = true,
GiphyContentRating = GiphyRatingType.Strict
}
};
var teamId = "71766077-aacc-470a-be5e-ba47db3b2e88";
await graphClient.Teams[teamId]
.Request()
.UpdateAsync(team);
// PATCH https://graph.microsoft.com/v1.0/teams/{team-id}
final Team team = new Team();
team.FunSettings = new TeamFunSettings();
team.FunSettings.AllowGiphy = true;
team.FunSettings.GiphyContentRating = GiphyRatingType.STRICT;
final String teamId = "71766077-aacc-470a-be5e-ba47db3b2e88";
graphClient.teams(teamId)
.buildRequest()
.patch(team);
You can use a Header() function to attach custom headers to a request. For PowerShell, adding headers is only possible with the Invoke-GraphRequest method. A number of Microsoft Graph scenarios use custom headers to adjust the behavior of the request.
// GET https://graph.microsoft.com/v1.0/users/{user-id}/events
var events = await graphClient.Me.Events
.Request()
.Header("Prefer",@"outlook.timezone=""Pacific Standard Time""")
.Select( e => new {
e.Subject,
e.Body,
e.BodyPreview
})
.GetAsync();
// GET https://graph.microsoft.com/v1.0/me/events
let events = await client.api('/me/events')
.header('Prefer','outlook.timezone="Pacific Standard Time"')
.select('subject,body,bodyPreview,organizer,attendees,start,end,location')
.get();
// GET https://graph.microsoft.com/v1.0/users/{user-id}/events
final Event events = graphClient.me().events()
.buildRequest(new HeaderOption("Prefer","outlook.timezone=\"Pacific Standard Time\""))
.select("Subject,Body,BodyPreview")
.get();
# GET https://graph.microsoft.com/v1.0/users/{user-id}/events
$userId = "71766077-aacc-470a-be5e-ba47db3b2e88"
$requestUri = "/v1.0/users/" + $userId + "/events"
$events = Invoke-GraphRequest -Method GET -Uri $requestUri `
-Headers @{ Prefer = "outlook.timezone=""Pacific Standard Time""" }
Important
The Microsoft Graph SDK for Go is currently in preview. Use of this SDK in production is not supported.
For SDKs that support a fluent style, you can provide custom query parameter values by using a list of QueryOptions objects. For template-based SDKs, the parameters are URL-encoded and added to the request URI. For PowerShell and Go, defined query parameters for a given API are exposed as parameters to the corresponding command.
//GET https://graph.microsoft.com/v1.0/me/calendarView
var queryOptions = new List<QueryOption>()
{
new QueryOption("startdate", "2020-12-01T00:00:00Z"),
new QueryOption("enddate", "2020-12-30T00:00:00Z")
};
var calendar = await graphClient.Me.CalendarView()
.Request(queryOptions)
.GetAsync();
// GET https://graph.microsoft.com/v1.0/me/calendar/calendarView
const start = '2020-01-01T19:00:00';
const end = '2020-01-07T19:00:00';
let calendar = await client
.api(`/me/calendar/calendarView?startDateTime=${start}&endDateTime=${end}`)
.get();
//GET https://graph.microsoft.com/v1.0/me/calendarView
final IEventCollectionPage calendar = graphClient.me().calendarView()
.buildRequest(new QueryOption("startdate", "2020-12-01T00:00:00Z"),
new QueryOption("enddate", "2020-12-30T00:00:00Z"))
.get();