8

I try to get a List of all properties from an Object in the Active Directory.

What I have for now is this:

List<User> users = new List<User>();
try
{
    DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
    root = new DirectoryEntry("LDAP://" + root.Properties["defaultNamingContext"][0]);
    DirectorySearcher search = new DirectorySearcher(root);
    search.Filter = "(&(objectClass=user)(objectCategory=person))";

    search.PropertiesToLoad.Add("samaccountname");
    search.PropertiesToLoad.Add("displayname");
    search.PropertiesToLoad.Add("mail");
    search.PropertiesToLoad.Add("telephoneNumber");
    search.PropertiesToLoad.Add("department");
    search.PropertiesToLoad.Add("title");

    SearchResultCollection results = search.FindAll();
    if (results != null)
    {
        foreach (SearchResult result in results)
        {
            foreach (DictionaryEntry property in result.Properties)
            {
                Debug.Write(property.Key + ": ");
                foreach (var val in (property.Value as ResultPropertyValueCollection)) { 
                    Debug.Write(val +"; ");
                }
                Debug.WriteLine("");
            }
        }
    }
}
catch (Exception ex)
{

}

But it gets only the properties I added with PropertiesToLoad. Is it possible to get all properties dynamically?

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
feargood
  • 83
  • 1
  • 1
  • 4
  • If you check out drew chapins answer here : https://stackoverflow.com/questions/23176284/difference-between-principalsearcher-and-directorysearcher You will know WHY you want to limit by propertiestoLoad. VeryFast vs VerySlow you make the call .. – Ken Jul 23 '20 at 15:56

2 Answers2

12

If you don't specify anything in PropertiesToLoad, you should get all the properties. Just remove the lines with search.PropertiesToLoad.Add.

Getting all the properties of all the users in the domain could be pretty heavy, however.

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
  • omg i tried it with PropertiesToLoad.Add(String.Empty), but I didn't expect it would be that simple. Thanks – feargood Jan 29 '15 at 12:53
  • 2
    This is super-late, but please *don't* return all the user properties. It's that kind of badly-constructed search that taxes AD. Given the fact that AD will include internal attributes that only make sense to Exchange, Skype etc, returning *all* gives a load of cruft that you'll never use. – LeeM Jul 20 '18 at 01:14
  • There are hundreds of properties, to load properties that not return by default, check the link https://learn.microsoft.com/en-us/windows/desktop/adschema/attributes-all and add to load list. – Larry Song Aug 23 '18 at 03:31
  • @trix If you check out drew chapins answer here : https://stackoverflow.com/questions/23176284/difference-between-principalsearcher-and-directorysearcher You will know WHY you want to limit by propertiestoLoad. VeryFast vs VerySlow you make the call .. – Ken Jul 23 '20 at 15:55
  • 1
    @Ken, I don't know whether you were meaning to address someone else, but I clearly stated *not* to return all properties. The comment at the start of the thread suggests *not* specifying `PropertiesToLoad` and I disagreed with that suggestion. – LeeM Jul 24 '20 at 16:40
  • @feargood If you check out drew chapins answer here : stackoverflow.com/questions/23176284/… You will know WHY you want to limit by propertiestoLoad. VeryFast vs VerySlow you make the call – Ken Jul 24 '20 at 18:32
  • @Trix yes .. meant for someone else - you were correct in your comment. When requesting data better to filter for what you want in the request and not after the request. Why ask for a boat load of wrenches when you simply need the one. Lots of overhead.. – Ken Jul 24 '20 at 18:34
1

To get all propertyNames you can do:

 SearchResult result = search.FindOne();
 ResultPropertyCollection temp = result.Properties;

 string[] propertyNamesList = new string[temp.PropertyNames.Count];
 temp.PropertyNames.CopyTo(propertyNamesList, 0);

and propertyNamesList will contain everything there.

Primoshenko
  • 140
  • 1
  • 9