'How to get ALL available properties for a DirectoryEntry

I have a reference to a user object, the Properties collection of this object will only contain properties that have values set but I need to check if a property (by name) exists for this object - I guess this would come from the schema.

I have looked at deUser.SchemaEntry, but I can't find any useful property info from this object.

Any ideas?

DirectoryEntry deUser = new DirectoryEntry(path);

foreach (var prop in deUser.Properties)
{
   //if user.Properties["company"] is not set on this user then 
   //it will not be available here although 'company' is 
   //a property defined for the user class
}

//How do I get to the list of all available properties using 
//deUserSchema as below
DirectoryEntry deUserSchema = deUser.SchemaEntry();


Solution 1:[1]

The problem turned out to be easier to solve than it appeared. In each object (DirectoryEntry) in AD there are dynamic properties named allowedAttributes and allowedAttributesEffective.

On standard retrieval by de.Attributes [], returns null. You must first force the rebuild of the object cache (de.RefreshCache) with these parameters. My code:

public static List<string> AllAvailableProperties(this DirectoryEntry de)
{
      de.RefreshCache(new string[] { "allowedAttributes" });
      return de.Properties["allowedAttributes"].AsStringList();
}

If we want a list of attributes for a class, we should take any object (existing) of this class.

Solution 2:[2]

To list all properties try this :

foreach (var name in deUser.Properties.PropertyNames)
{
   Console.WriteLine(name);
}

Solution 3:[3]

According to MSDN you can use DirectoryEntry.SchemaEntry to retrieve all attributes.

An entry's schema determines a list of its mandatory and optional property names. You can use this property to find out what properties and methods are available on the associated object.

String myADSPath = "LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";

// Creates an Instance of DirectoryEntry.
DirectoryEntry  myDirectoryEntry=new DirectoryEntry(myADSPath, UserName, SecurelyStoredPassword);

// Gets the SchemaEntry of the ADS object.
DirectoryEntry mySchemaEntry = myDirectoryEntry.SchemaEntry;

if (string.Compare(mySchemaEntry.Name,"container") == 0)
{
   foreach(DirectoryEntry myChildDirectoryEntry in myDirectoryEntry.Children)
   {
       //...do what you need
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Florian Winter
Solution 2 FolabiAhn
Solution 3 smr5