'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 1:[1]
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.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jim |
| Solution 2 |
