Delta Queries In Microsoft Graph API Using C#

Overview

 
Microsoft Graph is the gateway to data and intelligence in Microsoft 365. Using Microsoft Graph API it is easy to retrieve the entire set of data of a resource e.g: all the users or all the groups in an organization using simple get API calls. However, in certain business scenarios, you would like to see the new changes only - the Delta. This can be accomplished using the delta queries.
 
Delta query is very similar to a get API call but it is a separate function and it will get you all the current state so all the groups or all the emails in your mailbox Also at the end, it will give you what is called a Delta link and you can persist that link and you decide when you want to call again and you use the link to call again after a few seconds, few minutes, few days and the API is only going to return the new changes - the additions, the removal's, the modifications to the collection. So I call in a set of resources and after I finish paginating on the results I get a Delta link and with that Delta link after some time I can come back and get only the things that have changed since the last time the call was made.
 
In this article, I will walk you through the steps to execute delta queries in Microsoft using C# code. For the demo, code in this article tracks the changes made to user's profile properties in Azure Active Directory.
 
Prerequisites
  • App registered in Azure active directory(AAD) with necessary permissions granted to make a call.  

Package Dependency

 
Include below packages in your application to make a call to Graph API. 
  1. dotnet add package Microsoft.Identity.Client  
  2. dotnet add package Microsoft.Graph  
 Create GetGraphClient method to get the access token,
  1. private GraphServiceClient GetGraphClient()  
  2.     {  
  3.       var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {  
  4.         // get an access token for Graph  
  5.         var accessToken = GetAccessToken().Result;  
  6.   
  7.         requestMessage  
  8.             .Headers  
  9.             .Authorization = new AuthenticationHeaderValue("bearer", accessToken);  
  10.   
  11.         return Task.FromResult(0);  
  12.       }));  
  13.   
  14.       return graphClient;  
  15.     }  
 Add below method to retrieve the users with using delta query,
  1. private async Task<IUserDeltaCollectionPage> GetUsers(GraphServiceClient graphClient, object deltaLink)  
  2. {  
  3.   IUserDeltaCollectionPage page;  
  4.   
  5.   if (lastPage == null)  
  6.   {  
  7.     page = await graphClient.Users  
  8.                             .Delta()  
  9.                             .Request()  
  10.                             .GetAsync();  
  11.       }  
  12.   else  
  13.   {  
  14.     lastPage.InitializeNextPageRequest(graphClient, deltaLink.ToString());  
  15.     page = await lastPage.NextPageRequest.GetAsync();  
  16.   }  
  17.   
  18.   lastPage = page;  
  19.   return page;  
  20. }  
Add below method to display the users in the console with user Id, Name and Surname,
  1. private void OutputUsers(IUserDeltaCollectionPage users)  
  2. {  
  3.   foreach(var user in users)  
  4.   {  
  5.     var message = $"User: {user.Id}, {user.GivenName} {user.Surname}";  
  6.     Console.WriteLine(message);  
  7.   }  
  8. }  
 Add below method to retrieve the changes made to users profile properties and display it on the screen,
  1. private async Task CheckForUpdates()  
  2. {  
  3.   var graphClient = GetGraphClient();  
  4.   
  5.   // get a page of users  
  6.   var users = await GetUsers(graphClient, DeltaLink);  
  7.   
  8.   OutputUsers(users);  
  9.   
  10.   // go through all of the pages so that we can get the delta link on the last page.  
  11.   while (users.NextPageRequest != null)  
  12.   {  
  13.     users = users.NextPageRequest.GetAsync().Result;  
  14.     OutputUsers(users);  
  15.   }  
  16.   
  17.   object deltaLink;  
  18.   
  19.   if (users.AdditionalData.TryGetValue("@odata.deltaLink", out deltaLink))  
  20.   {  
  21.     DeltaLink = deltaLink;  
  22.   }  
  23. }  
Call Pattern for Delta Query
  • Step 1: Initiate a GET request with the delta function for the expected data from Microsoft 365
  • Step 2: Response is received with the relevant data and a state token. The response has either of the two parameters :
    • nextLink - There is additional data to be retrieved in the session. you would need to continue making requests using the nextLink to retireve all the pages of data until deltaLink is returned in the response.
    • deltaLink - It means that there is no more data, a complete set of data is retrieved. For future requests, use this link for the changes in the resource.
  • Step 3: When you need to retrieve the latest changes in the resource, use the deltaLink retrieved in Step 2. Response to this call will have delta changes and either of the parameters nextLink ordeltaLink.

Summary

 
Microsoft Graph API offers a wide range of resource collections through its endpoint. Based on the business scenario, the application might or might not need a complete set of data of a resource collection. In the scenarios of incremental data requests, delta queries are the best option to get only the necessary latest changes.


Similar Articles