Active Directory Display all properties in a table
Solution 1
This can be done using DirectoryEntry
, but I don't think the SearchResultCollection
has all fields.
Try to create a DirectoryEntry
for every search result, it should have all active directory properties:
DirectoryEntry entry = result.GetDirectoryEntry();
Also, note that in the active directory every property can have multiple values (like the MemberOf field), so you'll have to iterate them as well.
I've wrote a similar method, but I chose a List
with keys/values (it seemed more manageable over WCF. ILookup
would be optimal, but I couldn't get it to work here). Here it is, stripped from try/catch/using
var list = new List<KeyValuePair<string, string>>();
foreach (PropertyValueCollection property in entry.Properties)
foreach (object o in property)
{
string value = o.ToString();
list.Add(new KeyValuePair<string, string>(property.PropertyName, value));
}
Solution 2
You can iterate through all properties this way:
foreach (SearchResult searchResult in allResults)
{
foreach (string propName in searchResult.Properties.PropertyNames)
{
ResultPropertyValueCollection valueCollection =
searchResult.Properties[propName];
foreach (Object propertyValue in valueCollection)
{
Console.WriteLine("Property: " + propName + ": " + propertyValue.ToString());
}
}
}
Is that what you need?
Solution 3
Came across this thread while looking on how to do this myself. Instead i found a different way of doing it, and seems to be working fine.
return ((DirectoryEntry)UserPrincipal.Current.GetUnderlyingObject()).Properties.PropertyNames;
Its loading perfectly find in a Combobox anyhow. Just incase anyone else comes across this thread.
Comments
-
PeteT about 4 years
I am trying to achieve an LDAP query to gather all properties we have about our users without specifying the properties before hand, I would like to display this in a table so used the below code. This works if I uncomment the search.PropertiesToLoad.Add("cn"); line and will display any other properties I add in the same way but not when I do a full search for all properties.
DirectoryEntry myLdapConnection = createDirectoryEntry(); DirectorySearcher search = new DirectorySearcher(myLdapConnection); search.CacheResults = true; //search.PropertiesToLoad.Add("cn"); SearchResultCollection allResults = search.FindAll(); DataTable resultsTable = new DataTable("Results"); //add columns for each property in results foreach (string colName in allResults.PropertiesLoaded) resultsTable.Columns.Add(colName, colName.GetType()); //loop to add records to DataTable foreach (SearchResult result in allResults) { int tmp = result.Properties.Count; DataRow row = resultsTable.NewRow(); foreach (string columnName in search.PropertiesToLoad) { if (columnName.Equals("lastlogon")) { if (result.Properties.Contains(columnName)) row[columnName] = ConvertDate(result.Properties[columnName].ToString()); else row[columnName] = ""; } else { if (result.Properties.Contains(columnName)) row[columnName] = result.Properties[columnName][0].ToString(); else row[columnName] = ""; } } resultsTable.Rows.Add(row); } gridResults.DataSource = resultsTable;
The problem seems to be with
foreach (string colName in allResults.PropertiesLoaded) resultsTable.Columns.Add(colName, colName.GetType());
I expected this to loop all properties when no PropertiesToLoad had been specified but it doesn't is their a way to achieve what I want to.
I know I need a few try catches and other bits in the code as of yet, it's a rough draft.
-
Taersious almost 9 yearsThis might be more helpful to me if your comment related a UserPrincipal to a DirectorySearcher, or something relevant. I may just not know enough about this object, but I see no valid object type, and I cannot resolve this.
-
Michael almost 7 yearsThis is using the newer System.DirectoryServices.AccountManagement. So you would need to add a reference and using statement.