Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here i create a function to get all information of all users from active diractory
but i know to get only name i feel difficult to get other information
like
i need to get this information for all users
name, department, email, displayname, ou, ext, title

can u tell me how to do this here is my code


C#
string[] properties = new string[] { "fullname" };
            try
            {

                dataGridView1.ColumnCount = 3;
                dataGridView1.Columns[0].Name = "Name";
                dataGridView1.Columns[1].Name = "Department";
                dataGridView1.Columns[2].Name = "Email";
                                               
                    DirectoryEntry entry = new DirectoryEntry("LDAP://asdd");
                    DirectorySearcher mySearcher = new DirectorySearcher(entry);
                    mySearcher.Filter = "(&(objectCategory=person)(objectClass=user))";
                    string[] propertiesToLoad = new string[1] { "samaccountname" };
                    string[] propertiesToLoad1 = new string[1] { "name" };
                    using (SearchResultCollection results = mySearcher.FindAll())
                    {
                        foreach (SearchResult result in results)
                        {                           
                            string name = (string)result.Properties["name"][0];
                            string email = (string)result.Properties["mail"][0];
                            dataGridView1.Rows.Add(new string[] { name,email});                           
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Problem in the server");
                }
Posted
Updated 12-Sep-11 19:09pm
v2

1 solution

Try this to show all properties for an user.

C#
foreach (string var in result.Properties.PropertyNames)
{
  System.Diagnostics.Debug.WriteLine(string.Format("{0} : {1}", var, sr.Properties[var][0].ToString()));
}
 
Share this answer
 
Comments
kami124 13-Sep-11 3:18am    
here i done some code can u tell me how to mention the column here for each field

////////////////////////////////////////////////////////////////////
try
{
department = (string)result.Properties["givenName"][0];

dataGridView1.Rows.Add(department);

}
catch
{

}

////////////////////////////////////////////////////////////////////

try
{
department = (string)result.Properties["telephoneNumber"][0];

dataGridView1.Rows.Add(department);

}
catch
{

}

////////////////////////////////////////////////////////////////////
try
{
department = (string)result.Properties["title"][0];

dataGridView1.Rows.Add(department);
}
catch
{

}
Kim Togo 13-Sep-11 3:32am    
Sorry, I do not understand what you want.
But I can see that you use the same variable "department" for all 3 information's?
kami124 13-Sep-11 3:42am    
oki want to display record in different column
kami124 13-Sep-11 4:17am    
here this statement is used to write data in rows
kami124 13-Sep-11 4:18am    
dataGridView1.Rows.Add(email);
how to write in columns
col1, col2, col3

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