'Delta Query with delta token: Microsoft Graph SDK

I am working on change notifications for users using Microsoft Graph SDK in a .netcore application. I am using delta query to get the latest changes.

private static IUserDeltaCollectionPage? lastPage = null;
private async Task<IUserDeltaCollectionPage> GetUsers(GraphServiceClient graphClient, object? deltaLink)
{
  IUserDeltaCollectionPage page;

  if (lastPage == null || deltaLink == null)
  {
    page = await graphClient.Users
                            .Delta()
                            .Request()
                            .GetAsync();
  }
  else
  {
    lastPage.InitializeNextPageRequest(graphClient, deltaLink.ToString());
    page = await lastPage.NextPageRequest.GetAsync();
  }

  lastPage = page;
  return page;
}

The problem I am facing with this approach is that, if the application restarts for what ever reason the lastPage will be null. In which case the next time the application runs it will bring back all the users. I don't want to get all the users, I just want to get the changed users.

Is there a way to make a delta query with the latest delta link?

I tried the below but I am getting badly formed query error.

var queryOptions = new List<QueryOption>()
                    {
                        new QueryOption("$deltatoken", deltaLink?.ToString())
                    };

await graphClient.Users.Delta().Request(options).GetAsync();


Solution 1:[1]

You can ask for the latest deltaLink by adding $deltaToken=latest to the delta function and the response will contain a deltaLink and no resource data.

Then you need to use received deltaLink

var queryOptions = new List<QueryOption>()
{
    new QueryOption("$deltatoken", "latest")
};

var page = await graphClient.Groups.Delta().Request(options).GetAsync();
var users = page.CurrentPage.ToList();
if (page.NextPageRequest!=null)
{
    // get latest data
    page = await page.NextPageRequest.GetAsync();
}

Resources:

delta query

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2250152