How to get Outlook contact groups using MAPI?
Question
In Outlook 2010, you can create contacts and add them to groups. Is there any way to get the list of such groups and the contacts in them? Here's how I access the contacts:
var outlook = new Outlook.Application().GetNamespace("MAPI");
var folder = outlook.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
foreach (var curr in folder.Items.OfType<Outlook.ContactItem>())
{
...
}
I do not mean default contact folders, such as "Contacts" and "Suggested contacts".
Solution
The contact groups are represented by DistListItem Interface. DistListItem
interface has MemberCount
property and GetMember()
method to iterate through the group members.
var outlook = new Application().GetNamespace("MAPI");
var folder = outlook.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
foreach (var curr in folder.Items.OfType<DistListItem>())
{
Console.WriteLine(curr.DLName);
for (int memberIdx = 1; memberIdx <= curr.MemberCount; memberIdx++)
{
var member = curr.GetMember(memberIdx);
Console.WriteLine(member.Name);
}
}
OTHER TIPS
You can try this one
DataTable dt = new DataTable();
dt.Columns.Add("FirstName");
dt.Columns.Add("MiddleName");
dt.Columns.Add("LastName");
dt.Columns.Add("Email");
Microsoft.Office.Interop.Outlook.Items OutlookItems;
Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();
MAPIFolder Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
OutlookItems = Folder_Contacts.Items;
foreach (var item in OutlookItems)
{
var contact = item as ContactItem;
if (contact != null)
{
DataRow dr = dt.NewRow();
dr["FirstName"] = contact.FirstName;
dr["MiddleName"] = contact.MiddleName;
dr["LastName"] = contact.LastName;
dr["Email"] = contact.Email1Address;
dt.Rows.Add(dr);
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow