none
EWS Managed 1.1 - getting only those contacts that have email addresses (1, 2, or 3) RRS feed

  • Question

  • Hello

    The title says it all:  I'm looking for a searchfilter definition that will return only those contacts that have a value in one of the email address fields (or any combination thereof) and ignore all the others. I've tried using the Exist() clause but it doesn't seem to do anything worthwhile. 

    The structure below shows an initial searchFilter (for other stuff like that name prefix "a*") + the requirement for an 'existing' email address.

    I've written it such that filter1 is the prefix, it gets AND'd to filter2 (where the Or'd exist()  on emailaddress (1,2, 3) are) through filter3. Unfortunately, this query returns null (no contacts). No error, just no data. 

    ***snippet****

                            


                            SearchFilter.SearchFilterCollection searchFilterCollection2 = new SearchFilter.SearchFilterCollection(LogicalOperator.Or);
                            searchFilterCollection2.Add(new SearchFilter.Exists(ContactSchema.EmailAddress1));
                            searchFilterCollection2.Add(new SearchFilter.Exists(ContactSchema.EmailAddress2));
                            searchFilterCollection2.Add(new SearchFilter.Exists(ContactSchema.EmailAddress3));

    SearchFilter.SearchFilterCollection searchFilterCollection3 = new SearchFilter.SearchFilterCollection(LogicalOperator.And);

    searchFilterCollection3.Add(searchFilterCollection1);

                           searchFilterCollection3.Add(searchFilterCollection2);

                            contacts = GetContactsFolder(filter.Token).FindItems(searchFilterCollection3, view);

    Tuesday, May 8, 2012 1:18 PM

Answers

  • Even if the EmailAddress aren't set the properties will geneally exist but will be blank (you can confirm this using a MapiEditor). So i would try something like this which seems to work for me.

                SearchFilter sf1 = new SearchFilter.IsNotEqualTo(ContactSchema.EmailAddress1, "");
                SearchFilter sf2 = new SearchFilter.IsNotEqualTo(ContactSchema.EmailAddress2, "");
                SearchFilter sf3 = new SearchFilter.IsNotEqualTo(ContactSchema.EmailAddress3, "");
                SearchFilter sfSearch = new SearchFilter.SearchFilterCollection(LogicalOperator.And) { sf1, sf2, sf3 };
                FindItemsResults<Item> fiItems = service.FindItems(WellKnownFolderName.Contacts,sfSearch,  new ItemView(1000));
                foreach (Item itItem in fiItems.Items) {
                    if (itItem is Contact) {
                        Console.WriteLine(((Contact)itItem).DisplayName);
                        Console.WriteLine(((Contact)itItem).EmailAddresses[EmailAddressKey.EmailAddress1].Address);
                        Console.WriteLine(((Contact)itItem).EmailAddresses[EmailAddressKey.EmailAddress2].Address);
                        Console.WriteLine(((Contact)itItem).EmailAddresses[EmailAddressKey.EmailAddress3].Address);
                    }
                }
    Cheers
    Glen
    Wednesday, May 9, 2012 5:43 AM