Search code examples
c#emailexceptionexchangewebservicesmanaged

EWS - This operation can't be performed because one or more items are new or unmodified


Relating to a batch update question I asked previously about using a single update to mark as read all the unread emails, I could use ExchangeService.UpdateItems according to Jason's answer.

So I modified accordingly:

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;
        // update properties BEFORE ADDING
        iEM.IsRead = true;

        //DO NOT UPDATE INSIDE THE LOOP,I.E. DO NOT USE:
        //iEM.Update(ConflictResolutionMode.AutoResolve);

        //add the EmailMessage to the list
        emailsList.Add(iEM);
    });

    // fetches the body of each email and assigns it to each EmailMessage
    Service.LoadPropertiesForItems(emailsList,PropertySet.FirstClassProperties);

    // Batch update all the emails
    ServiceResponseCollection<UpdateItemResponse> response =
      Service.UpdateItems(emailsList,
       WellKnownFolderName.Inbox, ConflictResolutionMode.AutoResolve, 
       MessageDisposition.SaveOnly, null);
    // ABOVE LINE CAUSES EXCEPTION
    return emailsList;
}

Notice that I pulled up the IsRead attribution, placed it before adding the item to the list. The exception is the following:

This operation can't be performed because one or more items are new or unmodified

From MSDN's example it seems setting IsRead to true should be enough, so why are the items not being considered for the batch update?


Solution

  • At first guess it's the LoadPropertiesForItems call you make outside the loop. This likely overwrites your changes by loading the values back from the server.