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 thanItemSchema.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.