.netexchange-serverexchangewebservicesmanaged-ews

Retrieving emails from Exchange sorted by datetime received


When reading items from an Exchange mailbox, I'd like to be explicit in that they're picked up in order that they were received (datetime received) - earliest first.

How can I specify (on the ItemView?) the sort value(s), and sort direction?

using Microsoft.Exchange.WebServices.Data;

var service = new ExchangeService();
var inbox = new FolderId(WellKnownFolderName.Inbox);
var iv = new ItemView(500);

//how to specify sorting, if possible?

var items = service.FindItems(inbox, iv);
if (items.TotalCount > 0)
{
}

Using the Exchange Managed Web Services.


Solution

  • This example on MSDN shows how to use the OrderBy property of ItemView to sort results:

       iv.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);