Get members of an Exchange distribution list
This example prompts the user to select an Exchange distribution list from the Select Names dialog box and expands the distribution list to display its members.
Example
This code sample calls the GetExchangeDistributionListMembers method of the ExchangeDistributionList object to get an AddressEntries collection that contains all the members of the list. Because distribution lists can be nested inside another distribution list, the returned AddressEntries collection can represent any type of Exchange AddressEntry object.
Note
Expanding distribution lists can create a performance burden on an Exchange server, so use the GetExchangeDistributionListMembers method cautiously. Expect that your code will be slow when it expands large distribution lists.
To obtain the members of a distribution list, the user must be connected to an Exchange server and online.
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 Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.
Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub GetDistributionListMembers()
Dim snd As Outlook.SelectNamesDialog = _
Application.Session.GetSelectNamesDialog()
Dim addrLists As Outlook.AddressLists = _
Application.Session.AddressLists
For Each addrList As Outlook.AddressList In addrLists
If addrList.Name = "All Groups" Then
snd.InitialAddressList = addrList
Exit For
End If
Next
snd.NumberOfRecipientSelectors = _
Outlook.OlRecipientSelectors.olShowTo
snd.ToLabel = "D/L"
snd.ShowOnlyInitialAddressList = True
snd.AllowMultipleSelection = False
snd.Display()
If (snd.Recipients.Count > 0) Then
Dim addrEntry As Outlook.AddressEntry = _
snd.Recipients(1).AddressEntry
If (addrEntry.AddressEntryUserType = _
Outlook.OlAddressEntryUserType. _
olExchangeDistributionListAddressEntry) Then
Dim exchDL As Outlook.ExchangeDistributionList = _
addrEntry.GetExchangeDistributionList()
Dim addrEntries As Outlook.AddressEntries = _
exchDL.GetExchangeDistributionListMembers()
If Not (addrEntries Is Nothing) Then
For Each exchDLMember As _
Outlook.AddressEntry In addrEntries
Debug.WriteLine(exchDLMember.Name)
Next
End If
End If
End If
End Sub
private void GetDistributionListMembers()
{
Outlook.SelectNamesDialog snd =
Application.Session.GetSelectNamesDialog();
Outlook.AddressLists addrLists =
Application.Session.AddressLists;
foreach (Outlook.AddressList addrList in addrLists)
{
if (addrList.Name == "All Groups")
{
snd.InitialAddressList = addrList;
break;
}
}
snd.NumberOfRecipientSelectors =
Outlook.OlRecipientSelectors.olShowTo;
snd.ToLabel = "D/L";
snd.ShowOnlyInitialAddressList = true;
snd.AllowMultipleSelection = false;
snd.Display();
if (snd.Recipients.Count > 0)
{
Outlook.AddressEntry addrEntry =
snd.Recipients[1].AddressEntry;
if (addrEntry.AddressEntryUserType ==
Outlook.OlAddressEntryUserType.
olExchangeDistributionListAddressEntry)
{
Outlook.ExchangeDistributionList exchDL =
addrEntry.GetExchangeDistributionList();
Outlook.AddressEntries addrEntries =
exchDL.GetExchangeDistributionListMembers();
if (addrEntries != null)
foreach (Outlook.AddressEntry exchDLMember
in addrEntries)
{
Debug.WriteLine(exchDLMember.Name);
}
}
}
}