Search code examples
outlookoutlook-addin

outlook vsto add-in can't find contact


I'm trying to find a given contact using email for search email in a given folder folder using this method

    private static ContactItem FindContactByEmail(MAPIFolder folder, string email,string email2)
    {
        string filter = $"[Email1Address]='{email}'";
        $" used search filter [{filter}]".AddToLog();
        try
        {
            var result = folder.Items.Find(filter);// i tried folder.Items.Restrict(filter) too
            if (result!=null)
            {
                return result;
            }
            else
            {
                $"can't find {email} at {folder.FullFolderPath}".AddToLog();
            }
        }
        catch(System.Exception r)
        {
            $"exception at FindContactByEmail {r.Message}".AddToLog();
        }
        // If the contact was not found in the current folder, search in its subfolders
        foreach (MAPIFolder subfolder in folder.Folders)
        {
            var contact = FindContactByEmail(subfolder, email,email2);
            if (contact != null)
            {
                return contact;
            }
        }
        return null;
    }

i doubled checked that the contact exists in my folder but the method can't find it, it only find it at Recipient Cache folder missing all the contact details like phone numbers


Solution

  • If the contact was created from a GAL entry, Email1Address will contain the EX type address, not SMTP. You can either:

    1. Search for the EX address (AddressEntry.Address will give you that)
    2. Search for the SMTP address specific property on the contact - Email1OriginalDisplayName. It is not directly exposed by the ContactItem object, but you can see it in OutlookSpy (I am its author) if you click the Message button. It can be accessed in OOM through ContactItem.Property.GetProperty; the DASL property name is http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8084001F. If you need to search against that property using Items.Find/Restrict, you need to use the @SQL syntax:
     string filter = $"@SQL=""http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8084001F""='{email}'";