0
votes

I am using EWS to upgrade an application that used WebDAV to query an Exchange Server 2003 mailbox, the new version will work with Exchange Server 2010 SP2.

I want to exclude email items that have a subject that includes these search terms: "FATS;Assignment;Sandbox: Assignment"

I looked at MSDN Library: Searching for items in a mailbox by using the EWS Managed API and found out how to filter the recipients, and that the emails have attachments, but now I need to go a step further and only read emails that don't have the subjects above (4 different string exclusions)

here is the code I tried:

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

but I know (the last item) will only find items that contain those terms, how can I exclude those terms using a SearchFilter please?

1

1 Answers

1
votes

I found the answer on MSDN Library: Filtering on Not by using the EWS Managed API:

I have to use the SearchFilter.Not object:

searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "FATS")));

then I add that to the collection:

searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)));
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true)));

searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "FATS")));

// add all to the collection...
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());