none
Get Users from AD Group with more than 2000 users to gridview

    Question

  • Hi All,

    I have been using the below code to get the list of users from active directory for a selected group, this code pulls the details without any issue for the groups with users less than 1500 but gives an error if I select any group with more then 1500 users. Can any one help me how to get past this error.

     List<string> usersList = new List<string>();
                PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
                GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, DropDownList1.SelectedValue);
                
                DataTable dt = new DataTable();

                dt.Columns.Add("Description");
                dt.Columns.Add("DisplayName");
                dt.Columns.Add("SamAccountName");
                dt.Columns.Add("DistinguishedName");
                dt.AcceptChanges();

                foreach (Principal principal in group.Members)
                {
                    usersList.Add(principal.Name);

                    dt.Rows.Add(principal.Description.ToString(), principal.Name.ToString(), principal.SamAccountName.ToString(),principal.DistinguishedName.ToString());
                    dt.AcceptChanges();

                }

                grdViewAllADSUsers.DataSource = dt;
                grdViewAllADSUsers.DataBind();

    Thanks in advance

    Regards,

    Vamshi

    Tuesday, May 2, 2017 5:52 AM

Answers

  • My issue is fixed using the code below thanks for the support

    DirectoryEntry entry = new DirectoryEntry("LDAP://DC=TEST,DC=LOCAL");
                DirectorySearcher search = new DirectorySearcher(entry);
                string query = "(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(&(mail=*)))";
                search.Filter = query;
                search.PageSize = 4000;
                search.PropertiesToLoad.Add("name");
                search.PropertiesToLoad.Add("mail");

                SearchResultCollection mySearchResultColl = search.FindAll();

                DataTable results = new DataTable();
                results.Columns.Add("Employee ID");
                results.Columns.Add("Name");
                results.Columns.Add("Login ID");
                results.Columns.Add("Mail");
                results.Columns.Add("Designation");
                results.Columns.Add("Department");
                results.Columns.Add("Location");
                results.Columns.Add("Manager");
                results.Columns.Add("Created Date");

                int CurrRow = 0;

                foreach (SearchResult sr in mySearchResultColl)
                {
                    DataRow dr = results.NewRow();
                    DirectoryEntry de = sr.GetDirectoryEntry();
                    dr["Employee ID"] = de.Properties["description"].Value;
                    dr["Name"] = de.Properties["Name"].Value;
                    dr["Login ID"] = de.Properties["sAMAccountName"].Value;
                    dr["Mail"] = de.Properties["mail"].Value;
                    dr["Designation"] = de.Properties["title"].Value;
                    dr["Department"] = de.Properties["department"].Value;
                    dr["Location"] = de.Properties["physicalDeliveryOfficeName"].Value;
                    dr["Manager"] = de.Properties["manager"].Value;
                    dr["Created Date"] = de.Properties["whenCreated"].Value;

                    results.Rows.Add(dr);
                    de.Close();
                }
                grdViewAllADSUsers.DataSource = results;
                grdViewAllADSUsers.DataBind();

    Tuesday, May 9, 2017 4:34 AM

All replies

  • Take a look at this for how to loop the user information page by page.
    Tuesday, May 2, 2017 6:28 AM
    Answerer
  • Thanks for your reply cheong00 I tried this option but I am able to get any one attribute from AD either SamAccountName or DN like that but I want to get multiple attributes like Employee Name, Description, DN & SamAccountName
    Tuesday, May 2, 2017 7:17 AM
  • Have you tried adding those column names to properties variable (it's a string array) at line 12? Also remember that you'll need to add/change property filter at line 27 onwards to get the properties.
    Tuesday, May 2, 2017 9:19 AM
    Answerer
  • My issue is fixed using the code below thanks for the support

    DirectoryEntry entry = new DirectoryEntry("LDAP://DC=TEST,DC=LOCAL");
                DirectorySearcher search = new DirectorySearcher(entry);
                string query = "(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(&(mail=*)))";
                search.Filter = query;
                search.PageSize = 4000;
                search.PropertiesToLoad.Add("name");
                search.PropertiesToLoad.Add("mail");

                SearchResultCollection mySearchResultColl = search.FindAll();

                DataTable results = new DataTable();
                results.Columns.Add("Employee ID");
                results.Columns.Add("Name");
                results.Columns.Add("Login ID");
                results.Columns.Add("Mail");
                results.Columns.Add("Designation");
                results.Columns.Add("Department");
                results.Columns.Add("Location");
                results.Columns.Add("Manager");
                results.Columns.Add("Created Date");

                int CurrRow = 0;

                foreach (SearchResult sr in mySearchResultColl)
                {
                    DataRow dr = results.NewRow();
                    DirectoryEntry de = sr.GetDirectoryEntry();
                    dr["Employee ID"] = de.Properties["description"].Value;
                    dr["Name"] = de.Properties["Name"].Value;
                    dr["Login ID"] = de.Properties["sAMAccountName"].Value;
                    dr["Mail"] = de.Properties["mail"].Value;
                    dr["Designation"] = de.Properties["title"].Value;
                    dr["Department"] = de.Properties["department"].Value;
                    dr["Location"] = de.Properties["physicalDeliveryOfficeName"].Value;
                    dr["Manager"] = de.Properties["manager"].Value;
                    dr["Created Date"] = de.Properties["whenCreated"].Value;

                    results.Rows.Add(dr);
                    de.Close();
                }
                grdViewAllADSUsers.DataSource = results;
                grdViewAllADSUsers.DataBind();

    Tuesday, May 9, 2017 4:34 AM