Monday, February 1, 2010

The Object Already Exists

In our Active Directory set up the same username cannot exists twice within the domain which is pretty standard. However, the same name cannot exist either within the same organizational unit. I am working on a automated way to create new accounts so these little tidbits of information has come into play. When my application is trying to create an account with the same username OR name it throws a DirectoryServicesCOMException error with the same message "the object already exists." It's not very useful if you try to programmatically resolve it.

Upon examining the details of the exception object I notice that both times they are identical with one exception, the ExtendedError property which contains an integer value. So if the error is raised because the account tries to duplicate the username then the ExtendedError property is 1316, if it's just the name that already exists in the same OU then that number is 8305. Just in case you are wondering if both the username and the name are the same then the ExtendedError property is 1316 so directory services first checks on the username and then the name which makes perfect sense.

I hope this will help someone in need out there.