【问题标题】:EWS - This operation can't be performed because one or more items are new or unmodifiedEWS - 无法执行此操作,因为一个或多个项目是新的或未修改的
【发布时间】:2016-02-04 15:46:57
【问题描述】:

关于我之前提出的关于使用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 应该就足够了,那么为什么不考虑批量更新的项目呢?

【问题讨论】:

  • 仅供参考,阅读您的代码真是太难了
  • 你是不是不能放弃外循环,只对从 Service.FindItems(WellKnownFolderName.Inbox, sf, new ItemView(numOfMails2Fetch)) 返回的返回进行 foreach;
  • @Seabizkit 只有一个循环,我调整了代码以提高可读性。下面的Jason发现了问题,这是由于LoadPropertiesForItemsUpdateItems的顺序,我只是将加载方法放在后面(反转它),瞧,问题解决了。

标签: c# email exception exchangewebservices managed


【解决方案1】:

乍一看,这是您在循环之外拨打的LoadPropertiesForItems。这可能会通过从服务器加载值来覆盖您的更改。

【讨论】:

  • 再次感谢杰森!正是这样,我切换了这两行的顺序并按预期工作。大声笑,之前应该想到,加载是对列表先前状态的一种重置。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-04-27
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2022-07-19
    相关资源
    最近更新 更多