Ocaso Protal
Ocaso Protal

Reputation: 20257

Pagination with PrincipalSearcher

I need pagination when I use a PrincipalSearcher. I tried using the VirtualListView property of the underlying DirectorySearcher with no avail.

Samplecode:

using System;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Linq;
namespace Testbed
{
    internal class Testbed
    {
        private static void Main(string[] args)
        {
            Works();
            Fails();
            Console.ReadKey();
        }

        private static void Works()
        {
            var entry = new DirectoryEntry("LDAP://server/DC=example,DC=com", @"USERNAME", "PASSWORD");
            var searcher = new DirectorySearcher(entry);

            searcher.SearchScope = SearchScope.Subtree;
            searcher.Filter = "(cn=*)";
            // sort is needed, otherwise DVLV will not work?
            searcher.Sort = new SortOption("cn", SortDirection.Ascending);

            searcher.VirtualListView = new DirectoryVirtualListView(5, 10, 5);
            SearchResultCollection result = searcher.FindAll();
            Console.WriteLine("Works: " + result.Count);
        }

        private static void Fails()
        {
            var context = new PrincipalContext(ContextType.Domain, "server", "DC=example,DC=com",
                ContextOptions.Negotiate,
                @"USERNAME", "PASSWORD");

            var user = new UserPrincipal(context)
            {
                DisplayName = null,
            };

            var searcher = new PrincipalSearcher(user);
            ((DirectorySearcher) searcher.GetUnderlyingSearcher()).Sort = new SortOption("cn", SortDirection.Ascending);
            ((DirectorySearcher) searcher.GetUnderlyingSearcher()).VirtualListView = new DirectoryVirtualListView(0, 2, 2);
            PrincipalSearchResult<Principal> result = searcher.FindAll();

            Console.WriteLine("Fails: " + result.Count());
        }
    }
}

When I use DirectorySearcher (Method: Works()) directly everything works fine, but with PrincipalSearcher (Method: Fails()) I get teh following exception (sorry for the german text):

Ausnahmefehler: System.Runtime.InteropServices.COMException: Die angeforderte kritische Erweiterung wird vom Server nicht unterstützt.

   bei System.DirectoryServices.SearchResultCollection.ResultsEnumerator.MoveNext()
   bei System.DirectoryServices.AccountManagement.ADEntriesSet.MoveNext()
   bei System.DirectoryServices.AccountManagement.FindResultEnumerator`1.MoveNext()
   bei System.DirectoryServices.AccountManagement.FindResultEnumerator`1.System.Collections.IEnumerator.MoveNext()
   bei System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)

Any solution for this? Is this even possible with PrincipalSearcher?

Upvotes: 1

Views: 1429

Answers (1)

Ziru Zilbermond
Ziru Zilbermond

Reputation: 256

The default pagesize of PrincipalSearcher is set to 265. Try to set it to 0 like this:

((DirectorySearcher) searcher.GetUnderlyingSearcher()).PageSize = 0;

This should enable Pagination. The DirectorySearcher has a default PageSize of 0, which is why it works when you use it directly.

Upvotes: 4

Related Questions