How To Access Nested Outlook Distribution List Entry Through VSTO?

On the Microsoft exchange server, there can be multiple distribution list and each list can either directly contain users or it can contain another distribution list nested within it. When we create Outlook add-in, we may need to access these lists or outlook users programmatically. Here, I am describing couple of ways to access distribution list entry which is a part of another distribution list entry using Visual Studio Tools for office (VSTO).

The approach described here can be used while fetching first level DL or while fetching user members within any level DL. Both User and Distribution list are derived from AddressEntry class because of the obvious reason that members of Distribution list can be both user or another list. Derived Class for users is ExchangeUser and derived class for Distribution List is ExchangeDistributionList.

AddressEntryin turn is obtained from collection AddressEntries properties of AddressList object. Address Lists which contain all the Distribution lists for your exchange server can be obtained as shown below:

<pstyle="color: #333399; font-weight: bold;">var

outlook = <pstyle=”color: #008800; font-weight: bold;”>new

Outlook.Application().GetNamespace(<pstyle=”background-color: #fff0f0;”>”MAPI”

); Outlook.AddressLists addrLists = outlook.Application.Session.AddressLists;


Now Consider an example of exchange server where we have Distribution List Hierarchy as:

Nested Outlook Distribution List

For Instance, we have to fetch the Distribution list-‘Dev Team Delhi’, ‘Dev Team Mumbai’ and ‘Dev Team Chennai’– present inside the parent Distribution List ‘Dev Team’ list .

AddressLists (denoted by variableaddrLists in above code) does not contain any function to find the AddressList object. AddressLists can be obtained through iterating over complete collection. Sometimes Distribution lists can be thousands in number.

In our example of finding Members (Distribution list) for ‘Dev Team’ Distribution List, one of the approach can be:

<pstyle="color: #008800; font-weight: bold;">public

List<<pstyle=”color: #333399; font-weight: bold;”>string

> GetDevTeamByCity() { List<<pstyle=”color: #333399; font-weight: bold;”>string

> devTeamRegions = <pstyle=”color: #008800; font-weight: bold;”>new

List<<pstyle=”color: #333399; font-weight: bold;”>string

>(); <pstyle=”color: #008800; font-weight: bold;”>try

{ <pstyle=”color: #333399; font-weight: bold;”>var

outlook = <pstyle=”color: #008800; font-weight: bold;”>new

Outlook.Application().GetNamespace(<pstyle=”background-color: #fff0f0;”>”MAPI”

); Outlook.AddressLists addrLists = outlook.Application.Session.AddressLists; <pstyle=”color: #888888;”>// All Address List collections

<pstyle=”color: #008800; font-weight: bold;”>foreach

(Outlook.AddressList addrList <pstyle=”color: #008800; font-weight: bold;”>in

addrLists) { <pstyle=”color: #008800; font-weight: bold;”>if

(addrList.Name.Equals(<pstyle=”background-color: #fff0f0;”>”All Distribution Lists”

, StringComparison.OrdinalIgnoreCase)) { <pstyle=”color: #008800; font-weight: bold;”>foreach

(Outlook.AddressEntry addrEntry <pstyle=”color: #008800; font-weight: bold;”>in

addrList.AddressEntries) { <pstyle=”color: #008800; font-weight: bold;”>if

((addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry) && addrEntry.Name.Equals(<pstyle=”background-color: #fff0f0;”>”Dev Team”

, StringComparison.OrdinalIgnoreCase)) <pstyle=”color: #888888;”>// Parent DL of Dev Team across regions

{ <pstyle=”color: #008800; font-weight: bold;”>foreach

(Outlook.AddressEntry regionEntry <pstyle=”color: #008800; font-weight: bold;”>in

addrEntry.Members) { <pstyle=”color: #008800; font-weight: bold;”>if

(regionEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry) { <pstyle=”color: #888888;”>// Checked for type of DL

devTeamRegions.Add(regionEntry.Name); } } <pstyle=”color: #008800; font-weight: bold;”>return

devTeamRegions; } } } } } <pstyle=”color: #008800; font-weight: bold;”>catch

(System.Exception ex) { <pstyle=”color: #008800; font-weight: bold;”>throw

; } <pstyle=”color: #008800; font-weight: bold;”>return

devTeamRegions; }


The above code returns required list of Distribution list members in the form of list of strings. Here, we first find main Distribution List or AddressList named ‘All Distribution Lists’ from AddressLists collection of outlook. Then we iterate over it and find parent list ‘Dev Team’ for required Distribution lists. Internally, data is fetched from Outlook COM object. This COM object is responsible for interacting with Exchange server.

Also note, in the innermost loop, AddressEntryUserType is checked for type of olExchangeDistributionListAddressEntry to confirm it is of type ExchangeDistributionList before returning the list of string containing ‘Dev Team Delhi’,’Dev Team Mumbai’,’Dev Team Chennai’.

Once the required list is found we may have to delve deeper for finding user member in list. For instance, finding users in ‘Dev Team Delhi’. For this, the required check should be done for

olExchangeUserAddressEntry type and then AddressEntry can be typecasted to ExchangeUser Type as shown below:

 <pstyle="color: #008800; font-weight: bold;">if

(userEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry) { Outlook.ExchangeUser userInfo = userEntry.GetExchangeUser(); <pstyle=”color: #333399; font-weight: bold;”>var

Email = userInfo.PrimarySmtpAddress, <pstyle=”color: #333399; font-weight: bold;”>var

FirstName = userInfo.FirstName, <pstyle=”color: #333399; font-weight: bold;”>var

LastName = userInfo.LastName }


In the above example with multiple hierarchy of Distribution list, there were only three Distribution lists. But in real scenario, there can be thousands of Distribution lists which need to be traversed through forEach loop because each level can have multiple lists.

Here is a second approach to find the Distribution list contained within another Distribution list.

<pstyle="color: #333399; font-weight: bold;">var

outlook = <pstyle=”color: #008800; font-weight: bold;”>new

Outlook.Application().GetNamespace(<pstyle=”background-color: #fff0f0;”>”MAPI”

); Outlook.AddressLists addrLists = outlook.Application.Session.AddressLists; <pstyle=”color: #888888;”>// Same till here

Outlook.AddressList addrEntryMain = addrLists[<pstyle=”background-color: #fff0f0;”>”All Distribution Lists”

]; <pstyle=”color: #888888;”>// finding the main DL directly

Outlook.AddressEntries addEntries = addrEntryMain.AddressEntries; <pstyle=”color: #888888;”>// Getting AddressEntries property which is list of AddressEntry

Outlook.AddressEntry Silo = addEntries[<pstyle=”background-color: #fff0f0;”>”Dev Team”

]; <pstyle=”color: #888888;”>// Finding directly without foreach loop.

<pstyle=”color: #008800; font-weight: bold;”>foreach

(Outlook.AddressEntry addrEntry <pstyle=”color: #008800; font-weight: bold;”>in

Silo.Members) { <pstyle=”color: #008800; font-weight: bold;”>if

(addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType. olExchangeDistributionListAddressEntry) { devTeamRegions.Add(addrEntry.Name); } }


Same results can be obtained from this approach. Here, I directly access‘Dev Team’by addEntries [‘Dev Team’].

Concluding Points

The second approach does not require iterating over all the Distribution Lists, making code simpler and elegant. Also, in the first approach developer may tend to use nested ForEach loop to reach ‘Dev Team’ assuming that it is nested inside ‘Product Team’, while in our case it was directly accessible under ‘All Distribution Lists’. Hierarchy didn’t make any difference here. The approaches which I presented can be used to fetch first Level DL or nested DL or even while fetching the Users within DL. When you have too many DL’s, the second approach shows significant advantage.

You might also like