Quick Tip – Getting the name of your Exchange Organization using Visual Studio C#…

Not a long post this time, but something that I wanted to share with you all.

A number of you will know that, although I would not pretend to be a professional programmer (by any stretch of the imagination) – I do like to dabble.
Over this weekend I have been looking at the Exchange 2007 Segregation Tool with a view to rewrite a lot of its functionality in C#.

The reasons as to why I have decided to re-write the tool are not really relevant here, although I will say that when the tool is ready for release I will cover those reasons detail.
However for the moment one of the more pertinent motivations was down to the way in which the original tool detected Exchange Organizational configuration settings.

Those of you whom have downloaded and used the tool will know that prior to using the main Executable of the Exchange Segregation Tool; you had to run the “Start.vbs” script.

When executed this script would create 3 text files that contained the names of your Domain, CAS server and Exchange Organization. However since its release I have received a number of reports from readers of the site that although on the whole the script worked – on some Exchange installations the scripts would not execute correctly and therefore would not create the required files.

The upshot of this would cause problems for the Segregation Application when it executed; as due to the absence of these files it would throw an I/O 103 error as there is code logic for certain text fields within the Application which attempted to load the contents of the data files.

What I wanted to do, was re-write the application in .NET – but make all of its own internal configuration calls native to the application; rather than having to Execute external PowerShell scripts.
The first challenge that I encountered, and is the focus of this article – was how to programmatically retrieve the Exchange Organization name from Active Directory.

I had a look around the internet, but I could not find many articles which explained how to do this natively in C#, at least not without having to make use of WEBDAV or CDO libraries which I did not really wish to do.

After about an hour of surfing, I decided that I could make use of .NET’s native support for Active Directory via its “DirectoryServices” library – where the following is the code which I came up with:

using System.DirectoryServices;
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://rootDSE");
tboxDomainName.Text = dirEntry.Properties["defaultNamingContext"].Value.ToString();

// Lets get the Exchange Organization

string exOrgPath = "CN=Microsoft Exchange,CN=Services,CN=Configuration," + tboxDomainName.Text;
DirectoryEntry orgEntry = new DirectoryEntry("LDAP://"+exOrgPath);

if (orgEntry.Children != null)
foreach (DirectoryEntry child in orgEntry.Children)
tboxExchangeOrg.Text = child.Name.ToString();

tboxExchangeOrg.Text = tboxExchangeOrg.Text.Replace("CN=", "");

The above code snippet is taken from my Visual Studio .NET (C#) project.

The project makes use of the “System.DirectoryServices” reference assembly and header libraries (so you will need to ensure that you have added them to your project).
The code itself is taken from a “Button click” event on the main form – which executes the above code.

Upon being clicked the tboxDomainName (which is a text box) is populated with the “Default Naming Context” for the domain, the code then goes onto construct the path (in the string called “exOrgPath”) to the location of the Exchange Organization name within ADSI plus the Default Naming Context which results in the full path (CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=my,DC=domain,DC=Com)

I the search the child entries of that location – which when viewed in ADSI edit there is only one which is the msExchOrganization container – I then populate that value into another text box (called tboxExchageOrg) and remove the “CN=” from that string.

Not sure how much help this will be to people out there – and indeed there are perhaps better ways to accomplish the task – but I though that it was worth publishing the code as it I had not been very successful in trying to find an example myself

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.