CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2009
    Posts
    3

    Getting ActiveDirectory Listing

    Hi, I have a piece of code (below) which grabs a list of users on an Active Directory. However it only seems to return the first 1500 users. Does anybody know how to modify this code so it returns all the users?

    public ArrayList GetActiveADUsersInGroup()
    {
    ArrayList adUsersInGroup = new ArrayList();
    String queryString = "LDAP://CN=ALL Employees,OU=Distribution Lists,OU=Exchange Objects,OU=System Management,DC=xxx,DC=corp,DC=local";
    DirectoryEntry de = new DirectoryEntry(queryString);


    PropertyValueCollection members = (PropertyValueCollection)de.Properties["member"];

    foreach (object member in members)
    {
    String queryStringMember = "LDAP://" + member.ToString();
    DirectoryEntry deMember = new DirectoryEntry(queryStringMember);
    String windowsIdentityName = deMember.Properties["sAMAccountName"].Value.ToString();

    adUsersInGroup.Add(windowsIdentityName);
    }
    return adUsersInGroup;
    }

    Thanks in advance
    Srinu

  2. #2
    Join Date
    May 2009
    Location
    Finland
    Posts
    7

    Re: Getting ActiveDirectory Listing

    Hi,

    Basically the problem is: The System.DirectoryServices namespace contains a flaw that may cause the group membership list to become truncated if new members are added to a group that already contains more than 1000 or 1500 members. Depending on system. On a computer that is running Microsoft Windows Server 2003, the group membership becomes truncated to 1500 members plus the newly added members. See FIX: The group membership list becomes truncated when you add group memberships by using the .NET Framework System.DirectoryServices namespace

    You can use the Range retrieval to get all users, see code example in MSDN: Enumerating Members in a Large Group
    Last edited by Loldemar; May 23rd, 2009 at 04:20 PM. Reason: added more info for problem description

  3. #3
    Join Date
    May 2009
    Posts
    3

    Angry Re: Getting ActiveDirectory Listing

    Hi Loldemar,

    Thank you so much for your quick reply...i tried your soluation i mean using the range retrieval...but unfortunetly i dint get soluation with this for what i was looking for...i am desparte to get the soluation...Thank you once again for your response...i need to struggle to get the result if you had any other option please please kindly let me know

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Getting ActiveDirectory Listing

    They give you the code:

    Code:
    try
    {
        DirectoryEntry entry = new DirectoryEntry("LDAP://CN=My Distribution List,OU=Distribution Lists,DC=Fabrikam,DC=com");
        DirectorySearcher searcher = new DirectorySearcher(entry);
        searcher.Filter = "(objectClass=*)";
    
        uint rangeStep = 1000;
        uint rangeLow = 0;
        uint rangeHigh = rangeLow + (rangeStep - 1);
        bool lastQuery = false;
        bool quitLoop = false;
    
        do
        {
            string attributeWithRange;
            if(!lastQuery)
            {
                attributeWithRange = String.Format("member;range={0}-{1}", rangeLow, rangeHigh);
            }
            else
            {
                attributeWithRange = String.Format("member;range={0}-*", rangeLow);
            }           
            searcher.PropertiesToLoad.Clear();
            searcher.PropertiesToLoad.Add(attributeWithRange);
            SearchResult results = searcher.FindOne();
            searcher.Dispose();
            foreach(string res in results.Properties.PropertyNames)
            {
                System.Diagnostics.Debug.WriteLine(res.ToString());
            }
            if(results.Properties.Contains(attributeWithRange))
            {
                foreach(object obj in results.Properties[attributeWithRange])
                {
                    Console.WriteLine(obj.GetType());
                    if(obj.GetType().Equals(typeof(System.String)))
                    {
                    }
                    else if (obj.GetType().Equals(typeof(System.Int32)))
                    {
                    }
                    Console.WriteLine(obj.ToString());
                }
                if(lastQuery)
                {
                    quitLoop = true;
                }
            }
            else
            {
                lastQuery = true;
            }
            if(!lastQuery)
            {
                rangeLow = rangeHigh + 1;
                rangeHigh = rangeLow + (rangeStep - 1);
            }
        }
        while(!quitLoop);
    }
    catch(Exception ex)
    {
        // Handle exception ex.
    }
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    May 2009
    Posts
    3

    Smile Re: Getting ActiveDirectory Listing

    Hi dglienna,

    Thanks for reply....this soluation at last worked out for me....spl thanks Loldemar too.

    Regards
    srinu

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured