Hi,
I have the following code that will search through Active Directory and return users to a DataTable.
string[] rowArrary = new string[userProperties.Length]; foreach (SearchResult searchResult in results) { for (int i = 0; i < userProperties.Length; i++) { if (searchResult.Properties.Contains(userProperties[i])) { userDetails[i] = searchResult.Properties[userProperties[i]][0].ToString(); } rowArrary[i] = userDetails[i]; } table.Rows.Add(rowArrary); }
userProperties is an array of all the properties I am searching for in Active Directory and userDetails is the class I have for each returned employee. The code works, but the problem is because I'm putting it in an array, I cannot change the returned values in the class. For instance, the Manager vaule I have a function to modify the DN to show the DisplayName.
public string Manager { get { return _manager; } set { _manager = GetUserByDN(value); } }
Code I have for the GetUserByDN method.
public static string GetUserByDN(string dn) { SearchResult results = null; try { string path = LDAP://DOMAIN; DirectoryEntry entry = new DirectoryEntry(path); DirectorySearcher search = new DirectorySearcher(entry); //search.Filter = "(&(objectClass=user)(anr=" + fullCN + "))"; search.Filter = "(&(objectClass=user)(distinguishedname=" + dn + "))"; results = search.FindOne(); if (results != null) { search.PropertiesToLoad.Add(ADProperties.DISPLAYNAME); return results.Properties[ADProperties.DISPLAYNAME][0].ToString(); } else { return null; } } catch (Exception ex) { string debug = ex.Message; return null; }
I have other returned Active Directory properties I would like to modify. What is the best way to accomplish. I'm thinking I need to rework my code from the beginning.
Thanks,
A