peter
peter

Reputation: 21

GetDirectoryEntry not returning all properties

I'm trying to query AD to retrieve user data.

Query works fine, but when enumerating through the properties returned via GetDirectoryEntry I am not able to see all the attributes I am seeing in Active Directory Explorer.

Code snippet below:

offEntry = pResult.GetDirectoryEntry();
foreach (PropertyValueCollection o in offEntry.Properties)
{
    Debug.Print(o.PropertyName + " = " + o.Value.ToString());
}

I am seeing attributes like "displayName" and "SAMAccountName" etc, but not the attributes I really want, ex: "postalCode", "streetAddress".

I have tried searching for a solution for this specific problem but have come to a dead end. What am I missing???

Regards Peter

Upvotes: 2

Views: 3619

Answers (2)

Jan
Jan

Reputation: 333

This is an old, but maybe it helps someone else:

  1. Leaving PropertiesToLoad of DirectorySearcher Empty (no .add) will result in all properties that already contain a value.
  2. the SearchResult has a method for returning the actual DirectoryEntry: result.GetDirectoryEntry(). Which (I am still trying to prevent that) will load all Properties that have been filled with values.
  3. The DirectoryEntry has a "InvokeSet" Method to fill Properties that are not filled yet, after that they will be returned. (There is also a "InvokeGet" Method, havent tried that one out, but maybe it helps getting empty Properties)

Upvotes: 1

Qpirate
Qpirate

Reputation: 2078

You should be able to use a searcher class like this.

DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(sAMAccountName=" + userAccount + ")";
                search.PropertiesToLoad.Add("mail");
                SearchResult result = search.FindOne();

then load the properties you want via the line

            search.PropertiesToLoad.Add("mail");

then in your SearchResult you will be able to read the properties

Upvotes: 1

Related Questions