3
votes

I am trying to set up a sample for delta query using the .NET client library (version 1.4 from https://www.nuget.org/packages/Microsoft.Graph). Doing the initial calls is smooth:

var page = await _graphClient.Users.Delta().Request().GetAsync();

while (page.NextPageRequest != null)
{
  page = await page.NextPageRequest.GetAsync();
}

Getting the deltaLink after the while is still pretty obvious:

string deltaLink = (string)page.AdditionalData["@odata.deltaLink"];

But what is the correct way to later use this deltaLink? I haven't found the obvious method / builder that would allow me to continue at a later time by using the URL (my current solution is to use code from section "Send HTTP requests with the .Net Microsoft Graph client library" at https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md and cast the thing to UserDeltaCollectionResponse - at that point I can use the normal APIs again).

2

2 Answers

8
votes

I wrote some code to get the actual delta token string.

 Uri deltaUri = new Uri(delta.AdditionalData[deltaLinkKey].ToString());
 var queries = System.Web.HttpUtility.ParseQueryString(deltaUri.Query);
 string token = queries.Get("$deltatoken");

There is then a QueryOption object in the API that you can add to the request. So, your code from above would look something like this.

QueryOption deltaOption = new QueryOption("$deltaToken", deltaToken);
var page = await _graphClient.Users.Delta().Request(new[] { deltaOption }).GetAsync();
6
votes

You are correct; currently, there is no elegant way to use the delta link as the basis for a new request. You will need to save it yourself and create a custom HTTP request:

HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Get, deltaLink);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(hrm);
HttpResponseMessage response = await graphClient.HttpProvider.SendAsync(hrm);

If you are looking to use it again within the same application instance, you can use it more easily:

driveItemDeltaCollectionPage.InitializeNextPageRequest(graphClient, deltaLink.ToString());
driveItemDeltaCollectionPage = await driveItemDeltaCollectionPage.NextPageRequest.GetAsync();

There is currently an open issue on this library to add more intuitive support for delta links.