How to specify a SortOption in a DirectorySearcher?

  • Thread starter Thread starter Rudy Ko
  • Start date Start date
R

Rudy Ko

To All,

I'm trying to sort from a DirectorySearcher, but got "Incorrect
function" exception. Do you have a hint? The code is as following:

System.DirectoryServices.SortOption sortOption = new
System.DirectoryServices.SortOption("cn",
System.DirectoryServices.SortDirection.Ascending);

DirectorySearcher mySearcher = new DirectorySearcher(directoryWC);

mySearcher.Filter = "ou=people";

mySearcher.Sort = sortOption;


DirectoryEntry users=mySearcher.FindOne().GetDirectoryEntry();

If don't specify "sort option", the above code works.

Thanks for all the help!!!

Regards,

Rudy
 
I'm trying to sort from a DirectorySearcher, but got "Incorrect
function" exception. Do you have a hint?

Two, actually :-)

First of all, I would assume if you want to SORT on "cn", you will
also have to include the "cn" property in the "PropertiesToLoad"
collection!

And secondly - why would you even want to sort if you only retrieve a
single entry?? (with the .FindOne() method) Try using .FindAll() and
see if it works there....
SortOption sortOption = new SortOption("cn", SortDirection.Ascending);

DirectorySearcher mySearcher = new DirectorySearcher(directoryWC);

mySearcher.Filter = "ou=people";
mySearcher.Sort = sortOption;

// add the "cn" to the list of properties to include!!!
// and any others, too, of course, that you'd like to
// get access to
mySearcher.PropertiesToLoad.Add("cn");

Do a find all, in order to get more than one entry back (so that
sorting even makes sense!):

mySearcher.FindAll()

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top