Active Directory : PropertiesToLoad get all properties

28,577

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.

Share:
28,577

Related videos on Youtube

feargood
Author by

feargood

Updated on May 20, 2020

Comments

  • feargood
    feargood about 4 years

    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?

  • feargood
    feargood over 9 years
    omg i tried it with PropertiesToLoad.Add(String.Empty), but I didn't expect it would be that simple. Thanks
  • LeeM
    LeeM almost 6 years
    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.
  • Larry Song
    Larry Song almost 6 years
    There are hundreds of properties, to load properties that not return by default, check the link docs.microsoft.com/en-us/windows/desktop/adschema/… and add to load list.
  • Ken
    Ken almost 4 years
    @trix 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 ..
  • LeeM
    LeeM almost 4 years
    @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.
  • Ken
    Ken almost 4 years
    @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
    Ken almost 4 years
    @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..

Related