Yann F.
Yann F.

Reputation: 91

C# and Active Directory : test if an OU exist

How can i Test if and Organizational Unit Exists in Active Directory before creating it with C# ?

Upvotes: 1

Views: 5056

Answers (1)

marc_s
marc_s

Reputation: 755321

There's a .Exists() method on the DirectoryEntry which you can use - assuming you have the correct LDAP path for your OU!

if (DirectoryEntry.Exists("LDAP://" + objectPath))
{
   // ......
}

Your main problem will be: the path you're using is wrong - the Users is a generic container and thus needs to be addressed like this:

LDAP://192.168.0.1/CN=Users

Note the CN= prefix. If you had an actual organizational unit, it would have to be prefixed with OU=

For a great resource, check out Howto: (almost) everything in Active Directory

Upvotes: 6

Related Questions