0

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
gcresse
  • 115
  • 1
  • 12

1 Answers1

0

The DirectorySearcher.SizeLimit property is set to 1,000 by default. I found another StackOverflow question regarding this exact situation and apparently there are two workarounds. The StackOverflow answer that I am referring to is here: https://stackoverflow.com/a/3488957/1920035

David
  • 5,877
  • 3
  • 23
  • 40
  • I was getting 1232 users so I don't think it was the 1000 limit. I ended up adding (userAccountControl=512) to exclude disabled accounts and now I get about 800 rows returned, including the users that were missing. – gcresse Oct 17 '17 at 20:29
  • Oh, I'm sorry, I must have overlooked the "... more than 1,000 returned" statement somehow. Nonetheless, I'm glad that you were able to find a solution. – David Oct 18 '17 at 02:24