C# DirectorySearcher Exception (0x800700EA): More data is available

Hi,

I am getting an exception when I am running the following code. I would of thought
the limits I was setting would prevent it. So I am after any ways I can fix
the code to prevent the error from happening. To answer a question I will probably
get asked - yes I do need to do a large AD Search.

So any ideas?

Thanks,

Ward.
// Here is the exception generated:
//
// System.DirectoryServices.DirectoryServicesCOMException (0x800700EA): More data is available.
// 
//   at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.MoveNext()
//   at System.DirectoryServices.SearchResultCollection.get_InnerList()
//   at System.DirectoryServices.SearchResultCollection.get_Count()
//   at AD_Object_Discovery.Program.fnLDIFDE_Export_Containers(String starting_path)
//   


namespace AD_Object_Discovery        
{


    partial class Program
    {   
        // -------------------------------------------------------------------
        // fnSetSearcher_Settings: Set the searcher settings.
        // -------------------------------------------------------------------
        
        public static void fnSetSearcher_Settings(ref DirectorySearcher searcher)
        {
            searcher.PageSize = 1000;
            searcher.SizeLimit = 10000000;
        }
        
        
        public function fnLogException(Exception ex)
        {
        		Console.WriteLine(ex.ToString());
        }
        
       // -------------------------------------------------------------------
        // fnLDIFDE_Export_Containers: Export all containers to a file.
        // -------------------------------------------------------------------

        static void fnLDIFDE_Export_Containers(string starting_path)
        {
            try
            {

                fnWrite_Section("Active Directory Containers (objectClass=container)");

                DirectoryEntry BaseOU = new DirectoryEntry(starting_path);
                string filter = "(objectClass=container)";

                DirectorySearcher searcher = new DirectorySearcher(BaseOU, filter);

                fnSetSearcher_Settings(ref searcher);

                searcher.PropertiesToLoad.Add("cn");
                searcher.PropertiesToLoad.Add("description");
                searcher.PropertiesToLoad.Add("distinguishedName");
                searcher.PropertiesToLoad.Add("name");
                searcher.PropertiesToLoad.Add("objectGUID");
                searcher.PropertiesToLoad.Add("showInAdvancedViewOnly");


                SearchResultCollection ad_results = searcher.FindAll();

                foreach (SearchResult result in ad_results)
                {
                }

                ad_results.Dispose();
                searcher.Dispose();
                BaseOU.Dispose();

            }

            catch (Exception ex)
            {
                fnLogException(ex);
            }
        }
	}

}

Open in new window

LVL 1
whorsfallAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dshrivallabhCommented:
Use use System.DirectoryServices.Protocols to solve this problem

example can be found on

http://dunnry.com/blog/PagingInSystemDirectoryServicesProtocols.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shahan AyyubSenior Software EngineerCommented:
dshrivallabh is also correct.

I would like to suggest you this one also:

http://msdn.microsoft.com/en-us/library/system.directoryservices.searchresult.aspx
0
whorsfallAuthor Commented:
dshrivallabh,

Thanks for the response - can you help me out with the initilaization code.

I would like to be able to logon with the current users credentials and connect to a LDAP path.

I have posted a test program based on what you sent so you can tell me where I am going wrong.

Thanks.

Ward.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Net;

// Paging in System.DirectoryServices.Protocols
// http://dunnry.com/blog/PagingInSystemDirectoryServicesProtocols.aspx


namespace SDSPTest
{
    class Program
    {
        public static List<SearchResultEntry> fnPerformPagedSearch(
LdapConnection connection,
string baseDN,
string filter,
string[] attribs)
        {

            List<SearchResultEntry> results = new List<SearchResultEntry>();

            SearchRequest request = new SearchRequest(
                baseDN,
                filter,
                System.DirectoryServices.Protocols.SearchScope.Subtree,
                attribs
                );

            PageResultRequestControl prc = new PageResultRequestControl(500);

            //add the paging control

            request.Controls.Add(prc);

            while (true)
            {
                SearchResponse response = connection.SendRequest(request) as SearchResponse;
                //find the returned page response control
                foreach (DirectoryControl control in response.Controls)
                {

                    if (control is PageResultResponseControl)
                    {
                        //update the cookie for next set

                        prc.Cookie = ((PageResultResponseControl)control).Cookie;
                        break;
                    }
                }


                //add them to our collection

                foreach (SearchResultEntry sre in response.Entries)
                {
                    results.Add(sre);
                }

                //our exit condition is when our cookie is empty

                if (prc.Cookie.Length == 0)
                    break;

            }

            return results;
        }


        static void Main(string[] args)
        {
            string baseDN = "LDAP://DC=acme,DC=net";
            List<SearchResultEntry> ad_results = new List<SearchResultEntry>();
            LdapConnection connection = new LdapConnection(baseDN);
            // NetworkCredential credential = new NetworkCredential("user1", "password1", "fabrikam");
            NetworkCredential credential = new NetworkCredential("", "", "");

             string filter = "(objectClass=organizationalUnit)";

             string[] ad_attribs = new string[9];
             ad_attribs[0] = "name";
             ad_attribs[1] = "description";
             ad_attribs[2] = "objectGUID";
             ad_attribs[3] = "postalCode";
             ad_attribs[4] = "street";
             ad_attribs[5] = "st";
             ad_attribs[6] = "l";
             ad_attribs[7] = "c";
             ad_attribs[8] = "distinguishedName";

             
             ad_results = fnPerformPagedSearch(connection, baseDN, filter, ad_attribs);
        }
    }
}

Open in new window

0
whorsfallAuthor Commented:
Shahan_Developer,

Hi thanks for your response - I think I am missing it but how it different that you posted different from the
original method / code I am using.

Thanks,
Ward.
0
msmamjiCommented:
If you want to use the current logged on credentials, try commenting out the lines where you have explicitly defined the credentials.

// NetworkCredential credential = new NetworkCredential("", "", "");

Regards,
Shahid
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.