Code Sample: Retrieving all users in an OU

Using Directory Searcher. This sample shows how to get the results back quickly using
only the directorysearcher without binding to each individual directoryEntry. To do
this you must specify the properties you want to load before doing the search.

Dim searcher As DirectorySearcher
Dim results As SearchResultCollection
Dim searchRoot As New DirectoryEntry(“LDAP://CN=Users,DC=mytest,DC=whitworth,DC=org”)
searcher = New DirectorySearcher(searchRoot)
searcher.PropertiesToLoad.Add(“telephoneNumber”)
searcher.PropertiesToLoad.Add(“cn”)
searcher.Filter = “(&(objectClass=user)(objectCategory=person))”

results = searcher.FindAll

dim result as SearchResult
dim entryName as string = “”

dim entryTelephone as string = “”

for each result in results
entryName = result.properties(“cn”)(0).tostring

If Not result.Properties(“telephoneNumber”).Count = 0 then

entryTelephone = result.properties(“telephoneNumber”)(0).tostring
else

entryTelephone = “Empty”

end if

debug.writeline(entryName & vbTab & entryTelephone)
next

debug.writeline(“Complete”)

Random Posts

Loading…

Leave a Reply