Still have 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
I have also tried a variant with
mySearcher.Sort.PropertyName="sn"
mySearcher.Sort.Direction = "descending"
instead of the above sort syntax but it still doesn't work. I suspect that the sort code in fact is correct, since I have seen others using the exact same syntax examples but since I have no clue what the exception message "invalid function" could refer to, and why it occurs on the specific line I have marked out, I don't know where to look for what's causing the error. Without the sorting I get the list, but unsorted, and I really need it to be sorted...
