/* the following code shows how to get the userid from particular domain*/
public DataTable GetAllADDomainUsers(string domainpath)
{
ArrayList allUsers = new ArrayList();
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + domainpath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("givenname");
search.PropertiesToLoad.Add("sn");
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("UserId", typeof(string)));
dt.Columns.Add(new DataColumn("UserName", typeof(string)));
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
if (result.Properties.Contains("samaccountname"))
{
DataRow dr = dt.NewRow();
dr.BeginEdit();
dr[0] = ((String)result.Properties["samaccountname"][0]);
string FirstName = "";
String LastName = "";
if (result.Properties.Contains("givenname"))
{
FirstName = ((String)result.Properties["givenname"][0]);
}
if (result.Properties.Contains("sn"))
{
LastName = ((String)result.Properties["sn"][0]);
}
dr[1] = FirstName + " " + LastName;
dr.EndEdit();
if (FirstName != "" && LastName != "")
{
dt.Rows.Add(dr);
}
}
}//for
}
/* you will expect the full code soon */
I need some formatted things in this blog.
Thanks,
Subhas
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment