Search code examples
c#vstooutlook-addin

Outlook VSTO Add-In - Searching for Contacts by email address using DASL Table


I am trying to find the number of occurrences of specific email address in Outlook Contacts in all accounts, whether they are POP/IMPA or Exchange accounts. I did something similar where I detected number of emails in Inbox where 'To' email address matched the input email address.

Here is the function. It works fine when I remove the filter. So the problem is with the filter, however, I am unable to find the right Property value (the part that looks like this: 0x8083001F) I tied Dmitry Streblechenko's OutlookSpy at no avail. The row count that indicates the number of contact that have the input email address is always zero, despite that I have contacts with this email address...

I hope somebody can tell me the right hex Property.

`     private (int, int, int) SearchContactsInFolder(string emailAddress, Outlook.MAPIFolder Subfolder)//, BackgroundWorker worker)
      {
       using (Log.VerboseCall())
       {
           Log.VerboseFormat("Begin scanning contacts in {0}", Subfolder.Name);

           if (Subfolder.DefaultItemType != Outlook.OlItemType.olContactItem)
           {
               string msg = $"Skipping folder {Subfolder.Name} of type {Subfolder.DefaultItemType.ToString()}";
               Log.Verbose(msg);
               throw new Exception(msg);
           }

           Outlook.Table table = null;
           Outlook.Items contactItems = null;

           int toCount = 0;
           int ccCount = 0;
           int bccCount = 0;
           // 0x800F101F 0x39FE001E
           //string filterTo =  $"@SQL=\"http://schemas.microsoft.com/mapi/proptag/0x8083001F\" LIKE '%{emailAddress}%'";
           //string filterTo =  $"@SQL=\"http://schemas.microsoft.com/mapi/proptag/0x3A17001F\" = '{emailAddress}'";
           string filterTo = $"@SQL=\"http://schemas.microsoft.com/mapi/proptag/0x39FE001E\" = '{emailAddress}'";
           string filterCC =  $"@SQL=\"http://schemas.microsoft.com/mapi/proptag/0x8093001F\" = '{emailAddress}'";
           string filterBCC = $"@SQL=\"http://schemas.microsoft.com/mapi/proptag/0x80A3001F\" = '{emailAddress}'";

           try
           {
               contactItems = Subfolder.Items;

               if (contactItems != null && contactItems.Count > 0)
               {
                   int iOutlookContacts = contactItems.Count;
                   Log.VerboseFormat("Number of Outlook contacts {0} folder: {1}", Subfolder.Name, iOutlookContacts.ToString());

                   // Count occurrences in 'To'
                   table = Subfolder.GetTable(filterTo, OlTableContents.olUserItems);
                   if (table != null)
                   {
                       toCount = table.GetRowCount();
                       Marshal.ReleaseComObject(table);
                       table = null;
                   }

                   // Count occurrences in 'CC'
                   table = Subfolder.GetTable(filterCC, OlTableContents.olUserItems);
                   if (table != null)
                   {
                       ccCount = table.GetRowCount();
                       Marshal.ReleaseComObject(table);
                       table = null;
                   }

                   // Count occurrences in 'CC'
                   table = Subfolder.GetTable(filterBCC, OlTableContents.olUserItems);
                   if (table != null)
                   {
                       bccCount = table.GetRowCount();
                       Marshal.ReleaseComObject(table);
                       table = null;
                   }
               }
           }
           catch (Exception ex)
           {
               Log.Verbose(ex);
               throw;
           }
           finally
           {
               if (contactItems != null) Marshal.ReleaseComObject(contactItems);
               if (table != null) Marshal.ReleaseComObject(table);
           }

           return (toCount, ccCount, bccCount);
       }
   }`

Tried variety of Property codes. Tried Outlook Spy.

EDIT: Trying Outlook Spy again, but have no idea where the property name is buried: enter image description here

Maybe here? But it does not look like anything expected: enter image description here

EDIT: I did another search and found something under IMessage: By selecting Email1EmailAddress, Email2EmailAddress and Email3EmailAddress, I found DASL: Email: http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8083001F Email2: http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/8093001F Email3: http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/80A3001F

Is that what I need to use? The URL looks completely different from what I used previously for email items (having this long GUID as part of the URL), although the ending 0x tags are the same as I used before...


Solution

  • You are hardcoding a named property tag (http://schemas.microsoft.com/mapi/proptag/0x8093001F) - don't do that. See what OutlookSpy shows for that property in the DASL property name edit box.