EWS - 由于一个或多个项目是新的或未修改的,因此无法执行此操作

时间:2016-02-04 15:46:57

标签: c# email exception exchangewebservices managed

关于我之前询问的有关使用single update to mark as read all the unread emails的批量更新问题,我可以根据Jason's answer使用ExchangeService.UpdateItems

所以我做了相应的修改:

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

请注意,我提取了IsRead归因,在将项目添加到列表之前放置了它。例外情况如下:

  

由于一个或多个项目是新的或未修改的

,因此无法执行此操作

MSDN's example开始,将IsRead设置为true应该足够了,那么为什么这些项目没有被考虑用于批量更新?

2 个答案:

答案 0 :(得分:1)

首先猜测是你在循环之外进行的LoadPropertiesForItems调用。这可能会通过从服务器加载值来覆盖您的更改。

答案 1 :(得分:0)

这绝不是答案......我只是想检查一下你是否可以格式化它

    Folder.Bind(Service, WellKnownFolderName.Inbox);

    var sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

    //ItemView limits the results to numOfMails2Fetch items
    var foundItems = Service.FindItems(WellKnownFolderName.Inbox, sf, new ItemView(numOfMails2Fetch)).ToList();

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

    //List<EmailMessage> emailsList = new List<EmailMessage>();
    foreach(var item in foundItems)
    {
      var iEM = item as EmailMessage;
      if(iEM != null)
          iEM.IsRead = true;
      //emailsList.Add(iEM);
    }

    // Batch update all the emails
    var response = Service.UpdateItems(foundItems, WellKnownFolderName.Inbox, ConflictResolutionMode.AutoResolve, MessageDisposition.SaveOnly, null);

    return foundItems;
}