Get information about all distribution lists of which the current user is a member

This example uses the GetMemberOfList() method to get information about all distribution lists of which the current user is a member.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

In the following example, GetCurrentUserMembership calls the GetMemberOfList method to get an AddressEntries collection for all distribution lists of which the Exchange user is a member. If the user is not a member of any distribution lists, GetMemberOfList returns an AddressEntries collection that has a count of zero. The user must be online for GetMemberOfList to return an AddressEntries collection; otherwise, GetMemberOfList returns a null reference. GetCurrentUserMembership uses the GetExchangeUser() method, which returns the current ExchangeUser object, to test whether the user is online. Once the address entries are obtained, the example writes information about each of the user’s distribution lists to the trace listeners of the Listeners collection.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void GetCurrentUserMembership()
{
    Outlook.AddressEntry currentUser =
        Application.Session.CurrentUser.AddressEntry;
    if (currentUser.Type == "EX")
    {
        Outlook.ExchangeUser exchUser =
            currentUser.GetExchangeUser();
        if (exchUser != null)
        {
            Outlook.AddressEntries addrEntries =
                exchUser.GetMemberOfList();
            if (addrEntries != null)
            {
                foreach (Outlook.AddressEntry addrEntry
                    in addrEntries)
                {
                    Debug.WriteLine(addrEntry.Name);
                }
            }
        }
    }
}

See also