Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Outlook contact groups using MAPI?

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".

like image 938
Impworks Avatar asked Feb 20 '23 03:02

Impworks


1 Answers

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);
    }
}
like image 171
Sergey Vyacheslavovich Brunov Avatar answered Feb 26 '23 17:02

Sergey Vyacheslavovich Brunov