I am a newbie to .net and to Active DIrectory. I have cobbled together some code from various samples to retrieve a list of Names and Departments from Active Directory. This now works well.
I then query Active Directory again to determine the Manager of the selected Name. That almost works well, but what I get back from
DirectoryServices.SearchResult.Properties("Manager")(0)
gives me the full LDAP string. (CN=XXXX,OU=Users,OU=SYD - Sydney,OU=AU - Australia,......)
What I want is just the XXXX - not the whole string.
The same basic code worked when populating name and department into a combobox, so what could be the problem here?
I then query Active Directory again to determine the Manager of the selected Name. That almost works well, but what I get back from
DirectoryServices.SearchResult.Properties("Manager")(0)
gives me the full LDAP string. (CN=XXXX,OU=Users,OU=SYD - Sydney,OU=AU - Australia,......)
What I want is just the XXXX - not the whole string.
The same basic code worked when populating name and department into a combobox, so what could be the problem here?
Code:
Dim dEntry As New DirectoryServices.DirectoryEntry("LDAP://abc.xyz.com/OU=SYD - Sydney,OU=AU - Australia,DC=xyz,DC=com")
Dim dSearch As New System.DirectoryServices.DirectorySearcher(dEntry)
Dim sManager As Object
'define the filter
dSearch.Filter = "(&(objectCategory=person)(&(objectClass=user)(name=" & Me.cmbName.SelectedItem & ")))"
dSearch.SearchScope = DirectoryServices.SearchScope.Subtree
'define the properties to retrieve
dSearch.PropertiesToLoad.Add("Manager")
dSearch.Sort.Direction = DirectoryServices.SortDirection.Ascending
'dSearch.Sort.PropertyName = "Manager"
'Define a collection to populate
Dim cResult As DirectoryServices.SearchResultCollection
'Excute the query
cResult = dSearch.FindAll
Dim oRes As DirectoryServices.SearchResult
'query the collection and add the manager name to the textbox
For Each oRes In cResult
If Not (oRes.Properties("Manager")(0) Is Nothing) Then sManager = oRes.Properties("Manager")(0)
Me.txtManager.Text = sManager
Next