I have code to populate a user id dropdown list within a VB.net application. Some user names are not being returned. I'm getting more than 1000 returned so it does not appear to be the 1000 limit. If I add (sAMAccountName=Kry*) to the search filter, then the user (whose name starts with kry) that was not appearing does get returned. Any help on this would be greatly appreciated. Thanks!
Private Sub PopSecurityUser()
cboUser.Items.Clear()
Dim SearchRoot As DirectoryEntry = ActiveDirectory.Forest.GetCurrentForest.RootDomain.GetDirectoryEntry '< More portable. Discover domain root DirectoryEntry rather than hard coding a Global Catalog.
Dim AdObj As System.DirectoryServices.SearchResult
Dim Searcher As New DirectorySearcher(SearchRoot)
With Searcher
.PropertiesToLoad.Add("sAMAccountName")
.SearchScope = SearchScope.Subtree
.Filter = "(&(!objectClass=contact)(objectCategory=person)(objectClass=user))" '< Exclude contacts because they don't have a sAMAccountName property.
.ReferralChasing = ReferralChasingOption.All '< Causes lookups to follow LDAP referrals if the object doesn't exist on the queried domain controller.
End With
For Each AdObj In Searcher.FindAll
If Not IsNumeric(AdObj.Properties("sAMAccountName")(0).ToString.Substring(0, 1)) Then
cboUser.Items.Add(AdObj.Properties("sAMAccountName")(0))
End If
Next
cboUser.Sorted = True
End Sub