System.DirectoryServices.Protocols PageResultRequestControl TotalSize always returns 0 RRS feed

  • Question

  • I am trying to perform paginated search on Active Directory using System.DirectoryServices.Protocols.PageResultRequestControl.

    I do get the search results in pages, however, the searchResponse that I get does NOT have the correct TotalCount for total number of pages.

    Is it not supported? Or am I missing something here?

    This is sample code that I have used in order to implement above. I am using System.DirectoryServices.Protocols to query Active Directory.

    When PageResultRequestControl is added with page number, everything works perfectly except for totalSize.

    For example, in this code

    totalPageCount = 0 at all times, but the searchResult.Entries are different each iteration. (so query works fine)

    LdapConnection connection = new LdapConnection(ldapDirectoryIdentifier, credential);
    SearchRequest sr = new SearchRequest("", "(displayName=*)", System.DirectoryServices.Protocols.SearchScope.Subtree, new[] { "displayName"});
    PageResultRequestControl pr = new PageResultRequestControl(50);
    SearchOptionsControl so = new SearchOptionsControl(SearchOption.DomainScope);
    SearchResponse searchResponse;
    while (true)
        searchResponse = (SearchResponse)connection.SendRequest(sr);
        if (searchResponse.Controls.Length != 1 || !(searchResponse.Controls[0] is PageResultResponseControl))
            totalPageCount = 0;
            return null;
        PageResultResponseControl pageResponse = (PageResultResponseControl)searchResponse.Controls[0];
        totalPageCount = pageResponse.TotalCount;
        if (pageResponse.Cookie.Length == 0)
            pageRequest.Cookie = pageResponse.Cookie;
    Friday, June 8, 2018 2:33 PM