Initialize Delta Query with Delta Link
How can you initialise a delta query with a previous delta link?
Given this to grab messages
await graphServiceClient.Users[userEmail].MailFolders[mailBox].Messages.Delta().Request().GetAsync();
Next time around I want to use the deltaLink to start from just the new messages?
If you would like to query it again within the same application runtime, you can refer to the DeltaLinkDriveItem test.
If you would like to store the delta link and use it in a new query, you can create a custom HTTP request that overrides the default settings and navigates directly using the delta link:
HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Get, deltaLink);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(hrm);
HttpResponseMessage response = await graphClient.HttpProvider.SendAsync(hrm);
Feels a bit clunky but will give it a try, thanks.
Adding enhancement tag to create custom helper code for this
Here is a sample about how to get delta on messages, with the init case and the recurring cases:
var messageList = new List<Message>();
// Don't forget to define also graphServiceClient
// Here you get deltaLink that you stored from previous call => can be empty for 1st time
var deltaLinkValue = ... // Get your value where it was stored
IMessageDeltaCollectionPage messageCollectionPage;
// Get messages
// Init case (deltaLinkValue is empty)
if (String.IsNullOrWhiteSpace(deltaLinkValue))
{
messageCollectionPage = await graphServiceClient.Users[userEmail].MailFolders[mailBox].Messages.Delta().Request().GetAsync();
}
// If deltaLinkValue is not empty, you can call directly using the link, using "InitializeNextPageRequest"
else
{
messageCollectionPage = new MessageDeltaCollectionPage();
messageCollectionPage.InitializeNextPageRequest(graphServiceClient, deltaLinkValue);
await messageCollectionPage.NextPageRequest.GetAsync();
}
// Populate result
messageList.AddRange(messageCollectionPage.CurrentPage);
while (messageCollectionPage.NextPageRequest != null)
{
messageCollectionPage = await messageCollectionPage.NextPageRequest.GetAsync();
messageList.AddRange(messageCollectionPage.CurrentPage);
}
// Here you can get delta link for next call
if (messageCollectionPage.AdditionalData.TryGetValue("@odata.deltaLink", out object deltaLink))
{
// Store your deltaLink.ToString() somewhere, for step at the beginning of the method
}
return messageList;
nrobert provided the correct answer on how to use a cached delta link.
@nrobert Thanks for the example. I used it but kept getting the same delta over and over, I think there is a small issue with this code
messageCollectionPage = new MessageDeltaCollectionPage();
messageCollectionPage.InitializeNextPageRequest(graphServiceClient, deltaLinkValue);
await messageCollectionPage.NextPageRequest.GetAsync();
I made this change and looks like it fixed the issue
messageCollectionPage = await messageCollectionPage.NextPageRequest.GetAsync();
Is that OK or am I missing something?
Thanks, Moshe.
It is correct
@nrobert Thanks for the example. I used it but kept getting the same delta over and over, I think there is a small issue with this code
messageCollectionPage = new MessageDeltaCollectionPage(); messageCollectionPage.InitializeNextPageRequest(graphServiceClient, deltaLinkValue); await messageCollectionPage.NextPageRequest.GetAsync();I made this change and looks like it fixed the issue
messageCollectionPage = await messageCollectionPage.NextPageRequest.GetAsync();Is that OK or am I missing something?
Thanks, Moshe.
This saved me! Thanks so much @hugo972