Hiya, I'm trying to search AD based on either First name, Lastname or UserID, And populate a ListBox with theresults for a user to choose. When I test it using code similar to below, I am recieving duplicates of perhaps 1 or 2 users, when expecting a few hundred different ones. Is there something silly I am missing?
Dim oRoot As New DirectoryEntry(My LDAP Address)
Dim fnSearch As String = Replace(tbFirstName.Text, "'", "")
Dim lnSearch As String = Replace(tbLastName.Text, "'", "")
Dim uiSearch As String = Replace(tbUserID.Text, "'", "")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
Dim strFilter As String = "(&(objectClass=user) "
If fnSearch <> "" Then
strFilter = strFilter & "(givenname=" & fnSearch & "*)"
End If
If lnSearch <> "" Then
strFilter = strFilter & "(sn=" & lnSearch & "*)"
End If
If uiSearch <> "" Then
strFilter = strFilter & "(anr=" & uiSearch & ")"
End If
oSearcher.Filter = strFilter & ")"
Dim oResults As SearchResultCollection
'Try
oSearcher.PropertiesToLoad.Add("userPrincipalName")
oSearcher.PropertiesToLoad.Add("CN")
oResults = oSearcher.FindAll
lbNameList.Items.Clear()
If (Not oResults Is Nothing) Then
Dim UserID() As String
Dim LI As ListItem = New ListItem
For Each oResult In oResults
UserID = Split(oResult.Properties("userPrincipalName")(0), "@")
LI.Text = oResult.Properties("CN")(0)
LI.Value = UserID(0)
lbNameList.Items.Add(LI)
Next
lbNameList.SelectedIndex = 0
btnUse.Enabled = True
Else
lbNameList.Items.Clear()
btnUse.Enabled = False
End If