I need to programmatically retrieve all of the distinct values in Active Directory for a given field (for example, the country field "co" - but I will have others) I don't care how many times a given value occurs or who/what it is associated with - I just need the list of distinct countries currently in there.
I have this which will produce the list - but for obvious reasons it is unacceptably slow as it is querying every record in AD and adding new countries to the list as it discovers them.
Public Function GetAllCountries() As List(Of String)
Dim searcher As DirectorySearcher = ActiveDirectory.Forest.GetCurrentForest.FindGlobalCatalog.GetDirectorySearcher
Dim searchResults As SearchResultCollection
Dim properties As ResultPropertyCollection
Dim countryList As New List(Of String)
Dim country As String
Try
With searcher
.Filter = "(&(objectClass=user)(co=*))"
.PageSize = 1000
.SizeLimit = 0
.SearchScope = SearchScope.Subtree
.CacheResults = False
.PropertiesToLoad.Add("co")
searchResults = .FindAll
End With
For Each result As SearchResult In searchResults
properties = result.Properties
country = GetPropertyValue(properties("co")).SingleItem
If Not countryList.Contains(country) Then countryList.Add(country)
Next
Catch ex As Exception
End Try
Return countryList
End Function
FYI GetPropertyValue is just a custom function I put together to pull the string value from the Property object...
Is there any way of using DirectoryServices (or a suitable alternative?) to query distinct values for a specific AD field in a more efficient manner?