0

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);
        }
Jim
  • 113
  • 2
  • 11
  • You can try using this: Outlook.MAPIFolder myContactsFolder = application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); – Jon Feb 12 '16 at 15:07
  • try looking at the answer posted here at the bottom of this link.http://stackoverflow.com/questions/16884063/get-outlook-contacts-into-c-sharp-form-based-application also see if there is a specific API that's needed in `MS LOOK OUT 2016` – MethodMan Feb 12 '16 at 15:10
  • @MethodMan I will defiantly give that a go, and see how that goes. Thank you – Jim Feb 12 '16 at 15:28

2 Answers2

1

So the problem is that you cannot find the right AddressList object to assign to the SelectNamesDialog.InitialAddressList property?

You can go from the AddressList object to the MAPIFolder object using AddressList.GetContactsFolder, but unfortunately there is no corresponding MAPIFolder.GetAddressList method (unless you are using Redemption - I am its author - which implements RDOFolder2.GetAddressList), so the best you can do is loop through all address lists in the Namespace.AddressLists collection, call AddressList.GetContactsFolder. If you get back a valid MAPIFolder object, compare its entry id (MAPIFolder.EntryID) with the entry id of the default Contacts folder (Namespace.GetDefaultFolder(olFolderContacts)) using Namespace.CompareEntryIDs.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

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.

Jim
  • 113
  • 2
  • 11