msgraph-sdk-dotnet icon indicating copy to clipboard operation
msgraph-sdk-dotnet copied to clipboard

Initialize Delta Query with Delta Link

Open chronicbint opened this issue 9 years ago • 8 comments

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?

chronicbint avatar Jul 06 '17 15:07 chronicbint

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);

caitlinrussell avatar Jul 06 '17 16:07 caitlinrussell

Feels a bit clunky but will give it a try, thanks.

chronicbint avatar Jul 06 '17 17:07 chronicbint

Adding enhancement tag to create custom helper code for this

caitlinrussell avatar Jul 08 '17 00:07 caitlinrussell

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 avatar Aug 09 '17 15:08 nrobert

nrobert provided the correct answer on how to use a cached delta link.

MIchaelMainer avatar Aug 28 '17 20:08 MIchaelMainer

@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.

hugo972 avatar Jul 04 '19 13:07 hugo972

It is correct

ankitk20 avatar Aug 29 '19 06:08 ankitk20

@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

justmatt25 avatar Dec 29 '21 21:12 justmatt25