What is the easiest way to query the Active directory to get a strings list of departments names. Example: "Finance", "Marketing", "IT",etc. My case is an active directory for an enterprise with well over 3000 users.
+3
A:
Assuming that you just want to get a list of objects with the Department attribute returned you could use a DirectorySearcher in the System.DirectoryServices namespace.
Then your filter would be something like:
ds.Filter = "(objectClass=user)";
and you could then tell the searcher to just load the department attribute:
ds.PropertiesToLoad.Add("department");
Then enumerate throught the result set:
SearchResultCollection results = ds.FindAll();
Then add each department property to a Dictionary to get all the unique values
foreach (SearchResult result in results)
{
string dept = String.Empty;
DirectoryEntry de = result.GetDirectoryEntry();
if (de.Properties.Contains("department"))
{
dept = de.Properties["department"][0].ToString();
if (!dict.ContainsKey(dept))
{
dict.Add(result.Properties["department"][0].ToString();
}
}
}
Alternatively, there are command-line tools which will give you this information such as dsquery or adfind.
adfind -default -f "(objectclass=user)" department -list | sort
will give you a sorted list of the department attributes for all users.
benPearce
2009-06-28 23:49:42