Really could use some help on this. I am trying to query Active Directory for all users under the current manager that is signed in. Would anyone know how to code this? I have been unable to find any examples on how to do this.
I am still having a hard time trying to get this to work. I am using vb.net and I am looking for an example that shows how to query Active Directory for all users under a certain manager and then bind that query to a gridview. I have looked at both of
the suggestions above but I am not getting it. Could someone provide me with an example of how to go about this?
You will want to your filter to search for manager for exmple
(&(objectCategory=User)(manager=CN=Smith\, Bob))
Manager will be stored as the manager's distinquished name in the directory. So you will want to take the manager's samaccountname and do a query for his/her distinguishedname before you can do the search for all those who have him/her listed as a manager.
For example, make a function to pull the manger distinguishedname, then use that in your function to load their managed employees.
Dim ManagerDNstr as string = GetManagerName("bob.smith")
Thanks for the reply. Could you do a search to get all users based on a search.filter of the managers name and then go through those results and bind to a gridview or a dropdown list?
Here is the code that I am using. Not sure if it is correct -
Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://domain")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(Entry)
Dim oResults As SearchResultCollection
Entry.Username = Nothing
Entry.Password = Nothing
With oSearcher
.Filter = ("(&(manager=some name))")
End With
Dim colGroup As ArrayList
colGroup = New ArrayList
oResults = oSearcher.FindAll
'Dim Members As Object = oSearcher.FindAll
If oResults IsNot Nothing Then
For Each oResult In oResults
Dim strFName As String = oResult.Properties("givenName")(0).ToString
Dim strLName As String = oResult.Properties("sn")(0).ToString
colGroup.Add(strFName + " " + strLName)
'colGroup.Add(oResult.Properties("givenName")(0).ToString & " " & oResult.Properties("sn")(0).ToString)
Next
End If
colGroup.Sort() '<<< Sort by ascending
ddlReportsTo.DataSource = colGroup(0) '<<< Bind to Dropdown List
ddlReportsTo.DataBind()
Thanks for the reply. Could you do a search to get all users based on a search.filter of the managers name and then go through those results and bind to a gridview or a dropdown list?
Yes. You can use it just like any other datasource. But instead of an array you may want to put the results into a table and add that to a Dataset. Makes it easier to work with.
I have tried what you suggested gww, and I am not getting any results. I have posted my code below, can you tell me if you see anything that may be causing an issu?
Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://domain")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(Entry)
Dim dsUser As New DataSet
oSearcher.Filter = "(manager=some name)"
Dim oResults As SearchResultCollection
'Create a new table object within the dataset
Dim tbUser As DataTable = dsUser.Tables.Add("manager")
Dim rwDefaultUser As DataRow = tbUser.NewRow()
oResults = oSearcher.FindAll
If oResults IsNot Nothing Then
For Each oResult In oResults
rwDefaultUser("Full_Name") = oResult.Properties("givenName")(0).ToString & " " & oResult.Properties("sn")(1).ToString
rwDefaultUser("NT_Account") = oResult.Properties("sAMAccountname")(2).ToString
rwDefaultUser("Location") = oResult.Properties("physicalDeliveryOfficeName")(3).ToString
rwDefaultUser("Department") = oResult.Properties("department")(4).ToString
tbUser.Rows.Add(rwDefaultUser)
Next
Entry.Close()
End If
teklaw
Member
31 Points
20 Posts
Active Directory Question
Nov 30, 2011 12:43 PM|LINK
Really could use some help on this. I am trying to query Active Directory for all users under the current manager that is signed in. Would anyone know how to code this? I have been unable to find any examples on how to do this.
Thank you
bullpit
All-Star
21838 Points
4822 Posts
Re: Active Directory Question
Nov 30, 2011 01:44 PM|LINK
See if this helps:
http://stackoverflow.com/questions/190516/getting-all-direct-reports-from-active-directory
Max
Let Me Google That For You!
Horizon_Net
Star
8309 Points
1441 Posts
Re: Active Directory Question
Nov 30, 2011 02:04 PM|LINK
Hi,
normally you should find your answer (and many more) on the following site - http://www.codeproject.com/KB/system/everythingInAD.aspx
If my post solves your problem, please mark as answer.
teklaw
Member
31 Points
20 Posts
Re: Active Directory Question
Nov 30, 2011 03:03 PM|LINK
Thank you. I will take a look.
teklaw
Member
31 Points
20 Posts
Re: Active Directory Question
Nov 30, 2011 06:17 PM|LINK
I am still having a hard time trying to get this to work. I am using vb.net and I am looking for an example that shows how to query Active Directory for all users under a certain manager and then bind that query to a gridview. I have looked at both of the suggestions above but I am not getting it. Could someone provide me with an example of how to go about this?
Thank you for your help
gww
Contributor
2143 Points
458 Posts
Re: Active Directory Question
Nov 30, 2011 07:56 PM|LINK
You will want to your filter to search for manager for exmple
(&(objectCategory=User)(manager=CN=Smith\, Bob))
Manager will be stored as the manager's distinquished name in the directory. So you will want to take the manager's samaccountname and do a query for his/her distinguishedname before you can do the search for all those who have him/her listed as a manager. For example, make a function to pull the manger distinguishedname, then use that in your function to load their managed employees.
Dim ManagerDNstr as string = GetManagerName("bob.smith")
search.filter = "(&(objectCategory=User)(manager=" & ManagerDNstr & "))"
There doesnt appear to be a way to get a list of employees from the manager, like you can get a list of users that are a member of a group.
teklaw
Member
31 Points
20 Posts
Re: Active Directory Question
Nov 30, 2011 08:17 PM|LINK
Thanks for the reply. Could you do a search to get all users based on a search.filter of the managers name and then go through those results and bind to a gridview or a dropdown list?
teklaw
Member
31 Points
20 Posts
Re: Active Directory Question
Nov 30, 2011 08:21 PM|LINK
Here is the code that I am using. Not sure if it is correct -
Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://domain")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(Entry)
Dim oResults As SearchResultCollection
Entry.Username = Nothing
Entry.Password = Nothing
With oSearcher
.Filter = ("(&(manager=some name))")
End With
oSearcher.PropertiesToLoad.Add("givenName")
oSearcher.PropertiesToLoad.Add("sn")
oSearcher.PropertiesToLoad.Add("sAMAccountname")
oSearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName")
oSearcher.PropertiesToLoad.Add("department")
oSearcher.PropertiesToLoad.Add("manager")
Dim colGroup As ArrayList
colGroup = New ArrayList
oResults = oSearcher.FindAll
'Dim Members As Object = oSearcher.FindAll
If oResults IsNot Nothing Then
For Each oResult In oResults
Dim strFName As String = oResult.Properties("givenName")(0).ToString
Dim strLName As String = oResult.Properties("sn")(0).ToString
colGroup.Add(strFName + " " + strLName)
'colGroup.Add(oResult.Properties("givenName")(0).ToString & " " & oResult.Properties("sn")(0).ToString)
Next
End If
colGroup.Sort() '<<< Sort by ascending
ddlReportsTo.DataSource = colGroup(0) '<<< Bind to Dropdown List
ddlReportsTo.DataBind()
oSearcher.Dispose()
Any help with this code would be great.
Thank you in advance
gww
Contributor
2143 Points
458 Posts
Re: Active Directory Question
Nov 30, 2011 10:15 PM|LINK
Yes. You can use it just like any other datasource. But instead of an array you may want to put the results into a table and add that to a Dataset. Makes it easier to work with.
teklaw
Member
31 Points
20 Posts
Re: Active Directory Question
Dec 01, 2011 12:55 PM|LINK
I have tried what you suggested gww, and I am not getting any results. I have posted my code below, can you tell me if you see anything that may be causing an issu?
Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://domain")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(Entry)
Dim dsUser As New DataSet
oSearcher.Filter = "(manager=some name)"
Dim oResults As SearchResultCollection
'Create a new table object within the dataset
Dim tbUser As DataTable = dsUser.Tables.Add("manager")
Dim rwDefaultUser As DataRow = tbUser.NewRow()
tbUser.Columns.Add("Full_Name")
tbUser.Columns.Add("NT_Account")
tbUser.Columns.Add("Location")
tbUser.Columns.Add("Department")
Try
oSearcher.PropertiesToLoad.Add("givenName")
oSearcher.PropertiesToLoad.Add("sn")
oSearcher.PropertiesToLoad.Add("sAMAccountname")
oSearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName")
oSearcher.PropertiesToLoad.Add("department")
oSearcher.PropertiesToLoad.Add("manager")
oResults = oSearcher.FindAll
If oResults IsNot Nothing Then
For Each oResult In oResults
rwDefaultUser("Full_Name") = oResult.Properties("givenName")(0).ToString & " " & oResult.Properties("sn")(1).ToString
rwDefaultUser("NT_Account") = oResult.Properties("sAMAccountname")(2).ToString
rwDefaultUser("Location") = oResult.Properties("physicalDeliveryOfficeName")(3).ToString
rwDefaultUser("Department") = oResult.Properties("department")(4).ToString
tbUser.Rows.Add(rwDefaultUser)
Next
Entry.Close()
End If
Catch ex As Exception
End Try
Thank you for your help