1

I have a program that once every few hours synchronizes an Office 365 Online mailbox to a database collection. In order to pull down the latest messages, I'm using ExchangeService C# API

However, every once in a while, newest messages for certain contacts will not come thru. When I search for the messages via Outlook, it shows them no problem. But, the C# code that searches for messages by email ID does not find them.

Am I missing a criteria in my search strings? Or perhaps there is some caching going on - how do I disable it?

I usually have 10-20 messages per search result (this is not thousands)

Here's the relevant code:

        foreach (var item in customer.Contacts)
        {
            search.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.ToRecipients, item.Email));
            search.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.CcRecipients, item.Email));
            search.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.From, item.Email));
            search.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, item.Email));
            search.Add(new SearchFilter.ContainsSubstring(ItemSchema.DisplayTo, item.Email));
            search.Add(new SearchFilter.ContainsSubstring(ItemSchema.Body, item.Email));
        }

        var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, search.ToArray());

        var inboxCollection = _exchangeClient.FindItems(WellKnownFolderName.Inbox, filter,
                    new ItemView(1000) {PropertySet = PropertySet.FirstClassProperties});
Igorek
  • 15,716
  • 3
  • 54
  • 92

1 Answers1

0

That SearchFilter because of the number of ContainsSubstring searches is not going to be a very efficient search for the backend server to do. I would suggest you simply your query and just filter more of the results at the client side then try to use such a complex query. Eg you could use AQS search on the participants property and Body https://msdn.microsoft.com/en-us/library/office/dn579420(v=exchg.150).aspx which do the same thing as your query and would use the Indexes which would be much faster.

Why you seeing the behaviour your reporting is most likely because of the inefficient search and the way searching works, when you try a search like the one your using this would be a temporary restriction on the folder in question this is little old but explains it quite well https://technet.microsoft.com/en-us/library/cc535025(EXCHG.80).aspx .

Cheers Glen

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • Thank you! So, in order for me to accomplish this task, I would need to concatinate all the email addresses I'm looking for and pass them OR'ed together to the client? As in, would my query string be: "email1@email.com OR email2@email.com OR email3@email.com"? Would this search thru every possible property including subject and body and to/from? – Igorek Jan 22 '16 at 15:04