Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Directory : PropertiesToLoad get all properties

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?

like image 685
feargood Avatar asked Jan 29 '15 12:01

feargood


Video Answer


2 Answers

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.

like image 77
Paolo Tedesco Avatar answered Oct 22 '22 20:10

Paolo Tedesco


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.

like image 40
Primoshenko Avatar answered Oct 22 '22 22:10

Primoshenko