How to Use the .Net Microsoft Graph SDK to Get Users and Get Next Page of Results
Introduction
This post will explain how to use the .Net Microsoft Graph SDK in order to get users. This article is assuming that you have already gotten an Access Token using ADAL .NET. In order to learn more on how to get the Adal .NET token, please reference :
How to Use the ADAL .NET library to Acquire a Token Interactively in a Console Application (Authorization Code Flow) Link
Warning Regarding Microsoft Graph SDK
The Microsoft Graph SDK isn't updated as frequently as the Microsoft Graph API, which makes the Microsoft Graph SDK lag behind a bit. The most effective way to keep up with the Microsoft Graph API is to utilize the HTTPClient, and then parse the response body accordingly. If you would like to utilize the data structures and requests predefined in the Microsoft Graph SDK that is fine, however know that the SDK will not always be up to date with the Microsoft Graph API.
Installing the MSFT Graph SDK NuGet
In order to install this SDK, please go to your project management and then go to browse. Then look up Microsoft.Graph. This NuGet Package is shown in the picture below.
It is the Microsoft.Graph library.
Making Calls Using the Microsoft Graph SDK
In order to make calls to the Microsoft Graph SDK, you will need to get the Access token as shown in the other post on utilizing ADAL .net library to acquire a token. Once the access token has been acquired, you will create the graphserviceclient, setting the header of the request message as the access token. This is shown in the github gist below.
In the example, we get use the object in the graphserviceclient Users, which has a request call, which has the HTTP request call within, which can be called by awaiting the .getAsync(); method chain.
In order to set the OData parameters, you can add them after the request() call.
Expand, Skip, Top, OrderBy, and Filter are also supported via the client library when supported by the Microsoft Graph service for the request type.
For more information, please refer to the documentation for the MSFT Graph SDK here: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md
In order to page through the results, you will have to utilize the call : await users.NextPageRequest.GetAsync();
Where users is the GraphServiceClient Result.
Conclusion
This post has gone over how to install and utilize the Microsoft Graph SDK in order to get users from the Microsoft Graph API. We also review how to page through the Microsoft Graph SDK users in order to get the next set of users accordingly. If you have any issues with the Microsoft Graph SDK, please file an issue with them and the maintainers of the SDK will work to resolve the issue. https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues
If you have any issues with Azure Active Directory and making the Microsoft Graph API call, please file a support ticket with Azure and we will get back to you as soon as possible to resolve the issue.
Comments
- Anonymous
December 17, 2018
Please stop using .GetAwaiter().GetResult(); in Code samples and use async/await in a correct way.- Anonymous
December 17, 2018
Sure, what would you suggest is the correct way to do it? This is an easy one liner that customers are able to copy and paste, so it is more convenient.- Anonymous
December 31, 2018
var users = graphServiceClient.Users.Request().Top(5).GetAsync().GetAwaiter().GetResult();becomes var users = await graphServiceClient.Users.Request().Top(5).GetAsync();- Anonymous
December 31, 2018
I see, thanks for the constructive comments!- Anonymous
December 31, 2018
For future readers, this stackoverflow goes over the async await vs getawaiter difference : https://stackoverflow.com/questions/34549641/async-await-vs-getawaiter-getresult-and-callback
- Anonymous
- Anonymous
- Anonymous
- Anonymous