Problem with System.DirectoryServices.SortOption (LDAP)
I'm fetching an LDAP list of persons and it works fine except from that the result comes out unsorted. I want to sort by surname and have tried using System.DirectoryServices.SortOption. But when I add the sort code I get the exception: System.Runtime.InteropServices.COMException: Incorrect function, on the line where I call mySearcher.FindAll(). Without the SortOption lines it works fine. I'm not that familiar with DirectoryServices and LDAP so any help would be great!This is part of my code :
...
Dim entry As New DirectoryServices.DirectoryEntry("LDAP://xxx")
Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)
Dim result As System.DirectoryServices.SearchResult
Dim arrPerson As New ArrayList
Dim sortOption = New System.DirectoryServices.SortOption("sn", System.DirectoryServices.SortDirection.Descending)
mySearcher.Filter = ("sn=" & letter & "*") 'letter is a parameter containing the letter that the surname shall begin with
mySearcher.PropertiesToLoad.Add("uid")
mySearcher.PropertiesToLoad.Add("givenName")
mySearcher.PropertiesToLoad.Add("sn")
mySearcher.Sort = sortOption
For Each result In mySearcher.FindAll() 'here is where the exception occurs
arrPerson.Add(New Person(result.GetDirectoryEntry.Properties.Item("uid").Value, result.GetDirectoryEntry.Properties.Item("givenName").Value, result.GetDirectoryEntry.Properties.Item("sn").Value))
Next
