代码之家  ›  专栏  ›  技术社区  ›  Armfoot Ludovic Feltz

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

  •  1
  • Armfoot Ludovic Feltz  · 技术社区  · 8 年前

    关于我之前提出的关于使用 single update to mark as read all the unread emails ,我可以用 ExchangeService.UpdateItems 根据 Jason's answer .

    所以我做了相应的修改:

    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(IsRead) 设置为true就足够了,那么为什么不考虑这些项进行批量更新呢?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Jason Johnston    8 年前

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

        2
  •  0
  •   Seabizkit    8 年前

    这绝不是答案…我只是想看看你是否可以这样格式化它

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