user175084
user175084

Reputation: 4640

Get fully-qualified name of an OU

I have a code to get the list of OUs within a domain.

Now this just lists all the OUs and does not give any way to distinguish between an OU and a sub OU.

DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain);

DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("(objectClass=organizationalUnit)");

foreach (SearchResult temp in mySearcher.FindAll())
{
   OU_DownList.Items.Add(temp.Properties["name"][0].ToString());
}

Is there a way i can get the fully qualified name of an OU?

Something like this for a sub OU:

CN=Computer1,OU=Department 101,OU=Business Unit #1,DC=us,DC=xyz,DC=com

Any help is appreciated... thanks

Upvotes: 1

Views: 3393

Answers (2)

Jorge Ferreira
Jorge Ferreira

Reputation: 97937

Use the property Path from SearchResult, as in temp.Path, see link.

The Path property uniquely identifies this entry in the Active Directory hierarchy. The entry can always be retrieved using this path.

You can enumerate all the available properties with the following source code:

foreach(string propKey in temp.Properties.PropertyNames)
{
    // Display each of the values for the property identified by
    // the property name.
    foreach (object property in temp.Properties[propKey])
    {
        Console.WriteLine("{0}:{1}", propKey, property.ToString());
    }
}

Upvotes: 1

Brian Cauthon
Brian Cauthon

Reputation: 5534

temp.Path should get you the distinguishedName of each OU.

Upvotes: 5

Related Questions