Hi Kurt,
The DirectReports attribute contains the distinguishedName (DN) of the direct reports. With your code is splitting the DN to get the Name or cn component of the DN. The cn can be the same as the user's displayname but not always but the cn will be unique in an OU but not necessarily unique in the domain, however the DN will always be unique.
With the current query you are just returning the value of the directreports attribute, if you use a ASQ based query it can return the attributes of the DN entries, and can include the samaccountname or any other attribute from the single query.
To explain, with your current query you are doing this:
Search (cn=<fullname>) asking for directreports which returned the values in the attribute
CN=John Doe1,OU=Denmark,OU=Accounts,DC=MyDomain,DC=net
CN=John Doe2,OU=Denmark,OU=Accounts,DC=MyDomain,DC=net
CN=John Doe3,OU=Denmark,OU=Accounts,DC=MyDomain,DC=net
CN=John Doe4,OU=Denmark,OU=Accounts,DC=MyDomain,DC=net
With the ASQ query:
Search (DN of user) ASQ based on directreport asked for samaccountname and displayname which will return
CN=John Doe1,OU=Denmark,OU=Accounts,DC=MyDomain,DC=net; jdoe1; John Doe1
CN=John Doe2,OU=Denmark,OU=Accounts,DC=MyDomain,DC=net; jdoe2; John Doe2
CN=John Doe3,OU=Denmark,OU=Accounts,DC=MyDomain,DC=net, jdoe3; John Doe3
CN=John Doe4,OU=Denmark,OU=Accounts,DC=MyDomain,DC=net; jdoe4; John Doe4
The ASQ query is the most efficient method to return the attributes of user objects in the directreport attribute. So if you change your code to do something like this:
Search (cn=<fullname>) attribute return distinguishedName
Search (distinguishedName) ASQ Based on directreport, return samaccountname
As example here are the output of each, returning the values in the directreports
and with ASQ query based on the directreports attribute, asking for the samaccountname and displayname attributes.
Gary.