locked
List all directReports in active directory RRS feed

  • Question

  • I'm trying to list all directReports under a user into a listview. I got it to work but it is only showing the last person on the list and not all the people on the list. any help would be great. here is my code. This code is searching the directory using the user DN through lblUserDN.Text. then with it's properties look up directReports.

    Using user As New DirectoryEntry("LDAP://" & lblUserDN.Text <This is my user Domain name >)
    
    If user.Properties.Contains("directReports") Then
                            Dim reportTemp As String = ExtractUser(user.Properties("directReports")(0))
                            Listview1.Items.Add(reportTemp)
                        End If
    
    
        Private Function ExtractUser(ByVal username As String) As String
            Return Split(Split(username, "CN=")(1), ",")(0)
        End Function



    • Edited by vkid12 Tuesday, October 14, 2014 6:49 PM
    Tuesday, October 14, 2014 6:45 PM

Answers

  • Inside the loop, you have this stament

    Dim reportTemp As String = ExtractUser(user.Properties("directReports")(0))
    which is accessing the first element of user.Properties("directReports"). That is what the (0) means. You should be using the variable call prop which will change each time you go round the loop (the first time it will be the first PropertyValue, and the second time it will be the second PropertyValue).

    • Marked as answer by vkid12 Friday, October 17, 2014 10:15 PM
    Thursday, October 16, 2014 3:22 PM

All replies

  • I don't know if the rest of the code is correct, but this part:

    Dim reportTemp As String = ExtractUser(user.Properties("directReports")(0))
    Listview1.Items.Add(reportTemp)

    Takes the first (and only the first) element in the PropertyValueCollection returned by user.Properties("directReports") and assigns it to a single string which is added just once to the ListView. As the code is only executed once, you only have one item in the ListView. Perhaps you meant to loop through all the elements of the PropertyCollection, something like this:

    For each prop In user.Properties("directReports")
        'process the property
    Next


    • Edited by Blackwood Tuesday, October 14, 2014 7:07 PM
    • Proposed as answer by Paul Ishak Tuesday, October 14, 2014 7:29 PM
    Tuesday, October 14, 2014 7:00 PM
  • Wednesday, October 15, 2014 3:04 PM
  • I tried this and it only gave me the last person twice and not all the people directReports of that person.

    If user.Properties.Contains("directReports") Then
                            Dim prop As String
                            For Each prop In user.Properties("directReports")
                                Dim reportTemp As String = ExtractUser(user.Properties("directReports")(0))
                                ComboBox1.Items.Add(reportTemp)
    
                            Next
    
                        End If

    Thursday, October 16, 2014 3:10 PM
  • Inside the loop, you have this stament

    Dim reportTemp As String = ExtractUser(user.Properties("directReports")(0))
    which is accessing the first element of user.Properties("directReports"). That is what the (0) means. You should be using the variable call prop which will change each time you go round the loop (the first time it will be the first PropertyValue, and the second time it will be the second PropertyValue).

    • Marked as answer by vkid12 Friday, October 17, 2014 10:15 PM
    Thursday, October 16, 2014 3:22 PM
  • Yes thank you, I got it.
    Thursday, October 16, 2014 3:35 PM
  • If I put it in a listview, it goes : user02 | user 01

    can't I have it sorted and stay on one column and not two separate columns?

    Thursday, October 16, 2014 3:39 PM
  • Do you have the ListView View set to Details? You can either do that in the form designer, or you can do it in code

    ListView1.View = View.Details

    In the details view, each item should be on a separate row. If your names are still appearing in separate columns of the same row, you must have added them both as sub-items of the same item.

    You can sort the items in the ListView like this

    ListView1.Sort

    Thursday, October 16, 2014 4:22 PM
  • ok, problem solved, I used a listbox instead and everything works great. THanks.
    Thursday, October 16, 2014 4:22 PM