Find name of Exchange Organization in C#

In .NET C#, you can find the name your Exchange Organization using DirectoryEntry class from Configuration context store. Use the below C# code to get the name of your Exchange Organization by passing global catalog server.

//using System.DirectoryServices;
//-------------------------
private static string GetExchangeOrganizationName(string global_catalog_server)
{
    string organization = string.Empty;
    DirectoryEntry rootDSE = new DirectoryEntry("LDAP://" + global_catalog_server + "/rootDSE");
    string configDN = rootDSE.Properties["configurationNamingContext"].Value.ToString();
    string exOrgPath = "CN=Microsoft Exchange,CN=Services," + configDN;
    DirectoryEntry orgEntry = new DirectoryEntry("LDAP://" + global_catalog_server + "/" + exOrgPath);
    if (orgEntry.Children != null)
    {
        foreach (DirectoryEntry child in orgEntry.Children)
        {
            organization = child.Name.ToString().Replace("CN=", "");
            break;
        }
    }
    return organization;
}
Advertisement

Leave a Comment