Asked by:
How to Get the Canonical/Distinguished Name of a OU

Question
-
User-456111751 posted
Hi All,
I need your help in getting the CN/DN if the last part of the OU name is provided. For ex In our Active Directory domain of xyz.org which has OU of Accounts, sub OU Users and another Sub OU Managers (xyz.org>Accounts>Users>Managers)
Here If I provide Managers as an Input, it has to output the CN:
xyz.org/Accounts/Users/Managers
Something like below:
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://xyz.org"); DirectorySearcher ds = new DirectorySearcher(dirEntry); string objectName = "Managers"; ds.Filter = "(&(objectClass=organizationalUnit)(OUName(Notacorrect)=" + objectName + "))"; try { if (ds.FindOne() != null) { //Get the DN or CN name as said } } catch { }
Please help
Tuesday, February 5, 2013 10:00 AM
All replies
-
User-456111751 posted
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://xyz.org"); DirectorySearcher ds = new DirectorySearcher(dirEntry); ds.SearchScope = SearchScope.Subtree; string objectName = "Managers"; ds.PropertiesToLoad.Add("cn"); ds.Filter = "(&(objectClass=organizationalUnit)(ou=" + objectName + "))"; if (ds.FindOne() != null) { SearchResult deResult = ds.FindOne(); string ouName = deResult.Properties["cn"][0].ToString(); }
I get an exception message. Index was out of range on string ouName = deResult.Properties["cn"][0].ToString();
Please help
Tuesday, February 5, 2013 10:31 AM -
User-456111751 posted
Any help here...
Tuesday, February 5, 2013 11:44 PM -
User178204871 posted
Try this,
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://xyz.org");
DirectorySearcher ds = new DirectorySearcher(dirEntry);
ds.SearchScope = SearchScope.Subtree;
string objectName = "Managers";
ds.PropertiesToLoad.Add("canonicalname");
ds.Filter = "(&(objectClass=organizationalUnit)(name=" + objectName + "))";
if (ds.FindOne() != null) { SearchResult deResult = ds.FindOne(); string ouName = deResult.Properties["canonicalname"][0].ToString(); }
Tuesday, September 17, 2013 8:42 AM -
User1508394307 posted
Index was out of range on string ouName = deResult.Properties["cn"][0].ToString();Index was out of range most likely means that the value is not indexed, and need to be requested as
string ouName = deResult.Properties["cn"].ToString();
Thursday, September 19, 2013 6:27 AM -
User1508394307 posted
Did it help?
Wednesday, September 25, 2013 4:30 AM