John Snow
John Snow

Reputation: 1

C#: Unable to get all property names using DirectoryEntry

I have this code:

DirectoryEntry entry = new DirectoryEntry(_path + domain, domainAndusernam, password);
        try
        {
            Object obj = entry.NativeObject;
            string department, title;

            DirectorySearcher search = new DirectorySearcher(entry);

            search.Filter = "(SAMAccountName=" + username + ")";

            search.PropertiesToLoad.Add("cn");
            search.PropertiesToLoad.Add("department");
            search.PropertiesToLoad.Add("title");
            SearchResult result = search.FindOne();

            if (result == null)
            {
                return false;
            }
            _path = result.Path;
            _filterAttribute = (string)result.Properties["cn"][0];
            department = (string)result.Properties["department"][0];
            title = (string)result.Properties["title"][0];
            return true;
        }
        catch (Exception)
        {
            return false;
        }

My problem is, I can't seem to get all the property names from the SearchResult. I was able to get the "cn" and "mail", but not the other properties like "department" and "title". Is/are there something wrong here?

Upvotes: 0

Views: 645

Answers (1)

marc_s
marc_s

Reputation: 755216

If a property doesn't have a value assigned to it in AD, then that property doesn't appear in the result set. This is normal, expected behavior in AD code - if you can't find a property, it means that object doesn't have a value for that property in AD.

So if the "Department" for that particular user has not been filled in, you won't get an empty string as the "Department" value - the "Department" will instead just not be present in the result.Properties collection

Upvotes: 0

Related Questions