UserPrincipal GetUnderlyingObject: properties missing

Dänu picture Dänu · Feb 22, 2012 · Viewed 7k times · Source

I'm trying to load the attribute physicalDeliveryOfficeName from a DirectoryEntry which is returned by the GetUnderlyingObject method of a UserPrincipal instance:

DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;

which means that the following statement returns false:

directoryEntry.Properties.Contains("physicalDeliveryOfficeName");

I know that this property can be loaded by adding the name to the StringCollection DirectorySearcher.PropertiesToLoad when using said DirectorySearcher.

My questions are, why doesn't the DirectoryEntry returned by the method GetUnderlyingObject contain all properties? And how can I load this property without using a DirectorySearcher?

Answer

Avner Shahar-Kashtan picture Avner Shahar-Kashtan · Feb 22, 2012

Accessing all fields for a DirectoryEntry is a potentially slow and heavy operation. Some fields might not be replicated to all domain controllers, and so bringing the values might require accessing a remote and slow-to-access Global Catalog (GC) server.

Once you have a DirectoryEntry in hand and you want to pull a specific value, you can call the RefreshCache method, passing it the names of the properties you need.