none
How to use EWS SearchFilter to only find emails with attachments through service.finditems RRS feed

  • Question

  • Through EWS, I'm able to locate a folder and walk its emails while checking each one for attachments.  But, it would be faster if I can get the finditems method to ONLY return emails with attachments so I don't have to check each one as I walk through them.

    I've figured this is only a matter of specifying the correct SearchFilter so that only items with attachments are returned.  But, I have no idea how to design such a filter.  I've tried the following:

    SearchFilter.IsEqualTo sfEmailsWithAttachments = new SearchFilter.IsEqualTo();
    sfEmailsWithAttachments.PropertyDefinition = EmailMessageSchema.Attachments; //field to search on
    sfEmailsWithAttachments.Value = null;//value to check for
    But, EWS doesn't recognize this as a valid way to use the Attachments property in a search.  I've looked everywhere on the internet and haven't been able to find an example to filter attachments in EWS.  Everyone seems to walk the returned items and check each one for attachments.  If an inbox has thousands of emails, which only a few have attachments, this can take an uneccesary long time.

    Any ideas on how to restrict the search to only emails with attachments?



    Tuesday, October 9, 2012 7:25 PM

Answers

  • Found what I was doing wrong.

    First, finditems retrieves items and not just emails, so I had to switch the schema to ItemSchema.  This one has a HasAttachments property on it, that when set to true will cause the filter to find only items that have one or more attachments.  Setting it to false finds items that have no attachments.

    SearchFilter.IsEqualTo sfEmailsWithAttachments = new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, true);


    Also, found that finditem is designed to only return summary information that can then be used to decide whether to retrive the full item in a getitem command.  This was done by Microsoft to expidite searches on Exchange.  So, even though finditem will find only emails with attachements, the returned items may not neccesarily have things like counts for the attachments.  You would have to retrieve the whole email with getItem using the retrieved item's ID to do manipulations on any lower level fields.
    • Marked as answer by Infotek Andy Tuesday, October 9, 2012 8:38 PM
    Tuesday, October 9, 2012 8:36 PM

All replies

  • Found what I was doing wrong.

    First, finditems retrieves items and not just emails, so I had to switch the schema to ItemSchema.  This one has a HasAttachments property on it, that when set to true will cause the filter to find only items that have one or more attachments.  Setting it to false finds items that have no attachments.

    SearchFilter.IsEqualTo sfEmailsWithAttachments = new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, true);


    Also, found that finditem is designed to only return summary information that can then be used to decide whether to retrive the full item in a getitem command.  This was done by Microsoft to expidite searches on Exchange.  So, even though finditem will find only emails with attachements, the returned items may not neccesarily have things like counts for the attachments.  You would have to retrieve the whole email with getItem using the retrieved item's ID to do manipulations on any lower level fields.
    • Marked as answer by Infotek Andy Tuesday, October 9, 2012 8:38 PM
    Tuesday, October 9, 2012 8:36 PM
  • I know it might be a little late for that, but I would be happy to see the entire code :P
    Wednesday, February 25, 2015 4:04 PM