4

I followed the EWS Managed API example on MSDN to find all unread email in my Exchange mailbox account.

I later went through each found item in order to place them in the list I need to return while fetching the body of each message and updating each to IsRead=true as follows:

Folder.Bind(Service, WellKnownFolderName.Inbox);

SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
    new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
//ItemView limits the results to numOfMails2Fetch items
FindItemsResults<Item> foundItems = Service.FindItems(WellKnownFolderName.Inbox, sf,
    new ItemView(numOfMails2Fetch));

if (foundItems.TotalCount > 0)
{
    List<EmailMessage> emailsList = new List<EmailMessage>(foundItems.TotalCount);
    foundItems.Items.ToList().ForEach(item =>
    {
        var iEM = item as EmailMessage;
        emailsList.Add(iEM);
        // update properties
        iEM.IsRead = true;
        iEM.Update(ConflictResolutionMode.AutoResolve);
    });
    // fetches and assign the bodies of each email
    Service.LoadPropertiesForItems(emailsList,PropertySet.FirstClassProperties);
    return emailsList;
} else return null;

Is it possible to update all of the found items to IsRead=true in a single request instead? I.e. without updating them one by one = better performance and coherent logic.

Armfoot
  • 4,663
  • 5
  • 45
  • 60

1 Answers1

7

Yes, you can. ExchangeService.UpdateItems is the method you want to use here. See How to: Process email messages in batches by using EWS in Exchange for details.

Jason Johnston
  • 17,194
  • 2
  • 20
  • 34
  • Thanks Jason, I tried that method but it's giving me an exception. I asked a new question relating to it: [why the batch update is not considering the new items](http://stackoverflow.com/q/35205424/1326147). If you'd be so kind to figure out why, I can mark this as accepted. – Armfoot Feb 04 '16 at 15:49