Active Directory Query to Listview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Asprisa
    New Member
    • Oct 2007
    • 30

    Active Directory Query to Listview

    Hi All,

    Something weried happening when i try and return 4 AD properties to a listview box, here is my code:

    Code:
            
    Dim li As ListViewItem
    
    Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP://172.16.0.3/OU=Staff,OU=Users,OU=Accounts,OU=Network,DC=X,DC=local")
    
    dirEntry.Username = "x"
    dirEntry.Password = "x"
    
    Dim search As DirectorySearcher = New DirectorySearcher(dirEntry)
    search.Filter = "(&(objectClass=User)(objectCategory=person))"
    
    Dim res As SearchResultCollection = search.FindAll()
    
    For Each ItemRes As SearchResult In res
    
    li = Me.ListView1.Items.Add(ItemRes.Properties("displayname")(0).ToString())
    li.SubItems.Add(ItemRes.Properties("department")(0).ToString())
    li.SubItems.Add(ItemRes.Properties("homePhone")(0).ToString())
    li.SubItems.Add(ItemRes.Properties("mail")(0).ToString())
    
    Next
    Basically, if i only return the displayname, all the results come back, if i ask for the displayname and the mail as a subitem only 10 items come back, If i ask for department or homePhone to come back only 1 displayname is returned.

    If i return the items to messageboxes they return fine.

    Have i coded the subitems right on the listview?

    Thanks,

    James.
  • Asprisa
    New Member
    • Oct 2007
    • 30

    #2
    I managed to fix my own problem, the issue was if the property was empty it caused the rest of the query to go a bit loopy, so get around it i used this:

    Code:
    li = Me.ListView1.Items.Add(ItemRes.Properties("samaccountname")(0).ToString())
    
    If ItemRes.Properties.Contains("department") Then
    
    li.SubItems.Add(ItemRes.Properties("department")(0).ToString())
    
    Else
    
    li.SubItems.Add("Not in database")
    
    End If
    
    If ItemRes.Properties.Contains("homePhone") Then
    
    li.SubItems.Add(ItemRes.Properties("homePhone")(0).ToString())
    
    Else
    
    li.SubItems.Add("Not in database")
    
    End If
    
    If ItemRes.Properties.Contains("mail") Then
    
    li.SubItems.Add(ItemRes.Properties("mail")(0).ToString())
    
    Else
    
    li.SubItems.Add("Not in database")
    
    End If

    Comment

    Working...