DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

How to Access Nested Outlook Distribution List Entry Through VSTO?

Trying to access nested Outlook distribution lists? See how it's done using VSTO.

Anjani Singh user avatar by
Anjani Singh
·
Jan. 14, 17 · Tutorial
Like (0)
Save
Tweet
Share
7.24K Views

Join the DZone community and get the full member experience.

Join For Free

On the Microsoft exchange server, there can be multiple distribution lists and each list can either directly contain users or it can contain another distribution list nested within it. When we create an Outlook add-in, we may need to access these lists or Outlook users programmatically. Here, I'll describe a couple of ways to access a 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 the AddressEntry class because of the obvious reason that members of the Distribution list can be both user or another list. The derived class for users is ExchangeUser and the derived class for Distribution List is ExchangeDistributionList.

AddressEntry in 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:

var outlook = new Outlook.Application().GetNamespace("MAPI");
            Outlook.AddressLists addrLists = outlook.Application.Session.AddressLists;

Now, consider an example of an exchange server where we have a distribution list hierarchy as:

Distribution List Hierarchy


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 variable addrLists 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’ for the distribution list, one approach could be:

public List<string> GetDevTeamByCity()
        {
            List<string> devTeamRegions = new List<string>();
            try
            {
                var outlook = new Outlook.Application().GetNamespace("MAPI");
                Outlook.AddressLists addrLists =
                    outlook.Application.Session.AddressLists; // All Address List collections

                foreach (Outlook.AddressList addrList in addrLists)
                {
                    if (addrList.Name.Equals("All Distribution Lists", StringComparison.OrdinalIgnoreCase))
                    {
                        foreach (Outlook.AddressEntry addrEntry in addrList.AddressEntries)
                        {
                            if ((addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
                             && addrEntry.Name.Equals("Dev Team", StringComparison.OrdinalIgnoreCase)) // Parent DL of Dev Team across regions
                            {

                                foreach (Outlook.AddressEntry regionEntry in addrEntry.Members)
                                {
                                    if (regionEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
                                    { // Checked for type of DL
                                        devTeamRegions.Add(regionEntry.Name);
                                    }
                                }
                                return devTeamRegions;                               
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw;
            }
            return devTeamRegions;
        }

The above code returns required list of Distribution list members in the form of a list of strings. Here, we first find the 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 the type of olExchangeDistributionListAddressEntry to confirm it is of the type ExchangeDistributionList before returning the list of the 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 the 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:

 if (userEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry)
                                {
                                    Outlook.ExchangeUser userInfo = userEntry.GetExchangeUser();
                                      var  Email = userInfo.PrimarySmtpAddress,
                                      var  FirstName = userInfo.FirstName,
                                      var  LastName = userInfo.LastName
                                }

In the above example with multiple hierarchies of distribution list, there were only three distribution lists. But in a 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.

var outlook = new Outlook.Application().GetNamespace("MAPI");
            Outlook.AddressLists addrLists = outlook.Application.Session.AddressLists; // Same till here

            Outlook.AddressList addrEntryMain = addrLists["All Distribution Lists"]; // finding the main DL directly
            Outlook.AddressEntries addEntries = addrEntryMain.AddressEntries; // Getting AddressEntries property which is list of AddressEntry
            Outlook.AddressEntry Silo = addEntries["Dev Team"]; // Finding directly without foreach loop.
            foreach (Outlook.AddressEntry addrEntry in Silo.Members)
            {
                if (addrEntry.AddressEntryUserType ==
                       Outlook.OlAddressEntryUserType.
                       olExchangeDistributionListAddressEntry)
                {
                    devTeamRegions.Add(addrEntry.Name);
                }
            }

The 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 the code simpler and more elegant. Also, in the first approach, the 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 a significant advantage.

Distribution (differential geometry)

Published at DZone with permission of Anjani Singh. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Top 10 Best Practices for Web Application Testing
  • 10 Easy Steps To Start Using Git and GitHub
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: