Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searchFilter not working properly with EWS FindItems method call

I Am using a SearchFilter collection to restrict what emails are returned by a request to an Exchange 2010 mailbox using EWS.

I have no problem connecting to the service, and opening the mailbox.

The problem is that my searchFilter is being ignored, and all the emails are being returned by the request to EWS.

Here is my code:

static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;

//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(@"[email protected]");

// Find all items where the body contains "move reports".
//string qstring = "Body:\"move reports\"";

// Identify the item properties to return.
//view.PropertySet = new PropertySet(BasePropertySet.IdOnly,
//ItemSchema.Subject);

//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

//this will bind the mailbox you're looking for using your service instance
Folder inbox = Folder.Bind(service, fid);

List<SearchFilter> searchFilterCollection = new List<SearchFilter>();

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")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Assignment")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Sandbox: Assignment")));

SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());

ItemView view = new ItemView(100);

string sAttachmentPath = "C:\\Dev\\EWSHelloWorld\\attachments\\";

// Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

foreach (EmailMessage email in results)
// looping through all the emails
{

System.Diagnostics.Debug.Write("Found attachemnt on msg with subject: " + email.Subject);

.... code removed for brevity!

So, according to my understanding of the searchFilter, only unread emails with attachments should be returned and they should not have FATS or Sandbox: Assignment as the subject.

But that's not working, the request to EWS just returnes all the emails.

What am I doing wrong please?

like image 903
Our Man in Bananas Avatar asked Jan 10 '23 23:01

Our Man in Bananas


1 Answers

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.

like image 178
Bob Bunn Avatar answered Jan 17 '23 15:01

Bob Bunn