Search code examples
c#logicoutlook-addinoutlook-2016

How can you get user contact list in Outlook 2016 c#?


I am trying to display the contact list of an outlook account. (Outlook 2016) The following code displays the global contact list but not your own personal contact list. How can i show the account address list? This is code i have so far:

            try
            {    
                 Outlook._Application application = new Outlook.Application();

                 Outlook.AddressList addrList = null;

            foreach (Outlook.AddressList oAL in application.Session.AddressLists)
            {
                Outlook.MAPIFolder folder = oAL.GetContactsFolder();
            }

            Outlook.SelectNamesDialog dlg = application.Session.GetSelectNamesDialog();
            dlg.InitialAddressList = addrList;
            dlg.ShowOnlyInitialAddressList = true;
            dlg.NumberOfRecipientSelectors = Outlook.OlRecipientSelectors.olShowTo;

            dlg.Display();

            if (dlg.Recipients.Count > 0)
            {
                foreach (Outlook.Recipient recip in dlg.Recipients)
                {
                    Outlook.PropertyAccessor pa = recip.PropertyAccessor;
                    string smtpAddress = pa.GetProperty(PR_SMTP_ADDRESS).ToString();
                    AddrTextBox.Text += smtpAddress;
                    AddrTextBox.Text += "; ";
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

Solution

  • After, digging around researching and testing. I have found the answer to my own question. If you want to display the contact list of the specific account all you have to do is add an if statement in the first foreach statement:

     foreach (Outlook.AddressList oAL in m_AddInModule.OutlookApp.Session.AddressLists)
                {
                    Outlook.MAPIFolder folder = oAL.GetContactsFolder();
                     if (folder.AddressBookName == m_AddInModule.ContactsFolder.AddressBookName)
                     {
                         addrList = oAL;
                         break;
                     }
                }         
    

    If you add that into the code I have written in my initial post. You will succeed with seeing the contacts of the current account in outlook. I hope this will help you as it did me.