Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have to get list of users in a group of Active directory i have different group when user select the group name itshould fill the list of users belog to that group in gridview

i am using below code for getting the list of user but its not getting all the users in Active Directory

C#
try
       {

           string domainAndUsername = "domain" + @"\" + "admin";

           DirectoryEntry myLdapConnection = new DirectoryEntry(LdapPath, domainAndUsername, passwrd);
           DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=user)") };

           search.CacheResults = true;
           SearchResultCollection allResults = search.FindAll();
           DataTable resultsTable = new DataTable();
           resultsTable.Columns.Add("UserID");
           resultsTable.Columns.Add("EmailID");
           foreach (SearchResult searchResult in allResults)
           {



               MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties["sAMAccountName"][0].ToString()];

               if (myUser == null)
               {
                   DataRow dr = resultsTable.NewRow();
                   dr["UserID"] = searchResult.Properties["SAMAccountName"][0].ToString();
                   if (searchResult.Properties["mail"].Count > 0)
                   {
                       dr["EmailID"] = searchResult.Properties["mail"][0].ToString();
                   }
                   else
                   {
                       dr["EmailID"] = "";
                   }
                   resultsTable.Rows.Add(dr);
               }
               else
               { }
           }
           grdViewAllADSUsers.DataSource = resultsTable;
           grdViewAllADSUsers.DataBind();


       }
       catch (Exception)
       {
       }
Posted

1 solution

Probably users are not populating because after adding user your not performing acceptchanges operation on your datatable.

after (resultsTable.Rows.Add(dr))


so please update above code with this lines of code (Hear my assumption is that users are populated in searchresults (and AllResults) objects properly and problem with displaying data at runtime.

C#
try
        {
            
            string domainAndUsername = "domain" + @"\" + "admin";
           
            DirectoryEntry myLdapConnection = new DirectoryEntry(LdapPath, domainAndUsername, passwrd);
            DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=user)") };
        
            search.CacheResults = true;
            SearchResultCollection allResults = search.FindAll();
            DataTable resultsTable = new DataTable();
            resultsTable.Columns.Add("UserID");
            resultsTable.Columns.Add("EmailID");
            foreach (SearchResult searchResult in allResults)
            {
 
               
 
                MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties["sAMAccountName"][0].ToString()];
            
                if (myUser == null)
                {
                    DataRow dr = resultsTable.NewRow();
                    dr["UserID"] = searchResult.Properties["SAMAccountName"][0].ToString();
                    if (searchResult.Properties["mail"].Count > 0)
                    {
                        dr["EmailID"] = searchResult.Properties["mail"][0].ToString();
                    }
                    else
                    {
                        dr["EmailID"] = "";
                    }
                    resultsTable.Rows.Add(dr);
                }
                else
                { }
            }
               ///probable solution

            resultstable.AcceptChanges();

            grdViewAllADSUsers.DataSource = resultsTable;
            grdViewAllADSUsers.DataBind();
          
 
        }
        catch (Exception)
        {
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900