searchFilter not working properly with EWS FindItems method call

五迷三道 提交于 2019-12-01 08:01:13

Philip,

I started debugging your code and got a little confused as to what you are trying to get returned. In your code you are using an OR operator when you create the search filter, but in your text you describe the required output as

only unread emails with attachments should be returned and they should not have FATS or Sandbox: Assignment as the subject.

I took the parameters that you were trying to filter on and came up with the following filter that combines all the filters with a logical AND that works on my machine:

SearchFilter.SearchFilterCollection searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "FATS")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "Assignment")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "Sandbox: Assignment")));

FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilterCollection, new ItemView(100));

A few things to note:

  • I'm not using a generic list. I created a SearchFilterCollection with an AND logical operator and then added the filters to that collection.
  • My testing was with my local mailbox so I didn't bind to a different mailbox and get the folder id for the inbox. That shouldn't make a difference in your code though.
  • I used the EmailMessageSchema.Subject rather than ItemSchema.Subject. I also used my own string values in my testing but I placed your string values in the example.

When I ran my tests, if first filtered on unread messages with an attachment. I then verified that the returned results filtered further when I added the subject filter.

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!