Nivek
Nivek

Reputation: 21

How to display the value of a Search Result Collection from Active Directory in C#?

My boss sends me a txt file with some id numbers. I need to get some info from users that have those id numbers.

I've never done anything with Active Directory so I'm a little lost. Right now, I'm just trying to make sure that I can access the "muID" property from AD. But when I search AD for the property and try to get the value of the property I get an output of System.DirectoryServices.ResultPropertyValueCollection instead of the value, which should be similar to 111123456.

Here is my code so far:

SearchResultCollection sResults = null;
string path = "LDAP://muad1";

DirectoryEntry dEntry = new DirectoryEntry(path);
DirectorySearcher dSearcher = new DirectorySearcher(dEntry);

dSearcher.Filter = "(&(objectClass=user))";
dSearcher.PropertiesToLoad.Add("muID");

SearchResultCollection results = dSearcher.FindAll();

if (results != null)
{
    foreach (SearchResult result in results)
    {                    
        Console.WriteLine(result.ToString());
    }
}

But that's not working. I've tried searching around but I can't anything that works. I've tried this

Console.WriteLine(result.Properties["muID"].ToString());

Console.WriteLine(dEntry.Properties[result].ToString());

Console.WriteLine(dEntry.Properties[result][0].ToString());

Console.WriteLine(dEntry.Properties["result"].ToString());

but none of them work. They either throw an error or do the same thing that the one on the code block does.

Again, I want to make sure that I'm accessing that property so I can then get the info that I want. I thought displaying the value of the property would be a good way to check. But it is not displaying the right thing.

Upvotes: 2

Views: 4703

Answers (1)

KSib
KSib

Reputation: 921

Oh I feel like you're so close.

A SearchResultCollection contains SearchResult instances: Info on SearchResultCollection

A SearchResult contains a Properties property: Info on SearchResult

SearchResult Properties is a ResultPropertyCollection: Info on ResultPropertyCollection

If you update your code from

Console.WriteLine(result.ToString());

to:

Console.WriteLine(result.Properties["muID"][0].ToString());

then you should find what you're looking for. To be safe I would also make sure before accessing the element at [0], you check to make sure it exists first. You might run into an exception if a user doesn't have that property at all.

Edit: This might help you to see what property and value pairs are available for you to use. If you don't see "muID" anywhere then that's why you're getting an error.

if (results != null)
{
    foreach (SearchResult result in results)
    {                    
        foreach(string propName in result.Properties.PropertyNames)
        {
            foreach(object myCollection in result.Properties[propName])
            {
                Console.WriteLine(propName + " : " + myCollection.ToString());
            }
        }
    }
}

Upvotes: 2

Related Questions