Retrieve Data from Active Directory...

Hi,
I populat DataSet with 2 columns by retrieve data from AD.
The first column is CN (commonName) and the second is a property of the CN (some id).
What i get at the Grid is : |  CN         |       ID                    |
                                        | ----------------------------------|
                                        |  100        |   System.Object[]  |

I wrote in order to get it :      string sql = @"SELECT CN, ID  FROM 'LDAP://Domain'   WHERE CN =100'";
I understand that when i called the ID field , i actually called an object, but now, how can i get to the properties of that object (The ID value)??
Thanks
LVL 1
udirAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kate12Commented:
Have you consider using System.DirectoryServices?

using (DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName"))
            {
                foreach (string propertyName in entry.Properties.PropertyNames)
                {
                    System.Object value = entry.Properties[propertyName].Value;
                    if (value != null)
                    {
                        Console.WriteLine(value);
                    }
                    // If it is multiple values
                    if (value is IEnumerator)
                    {
                        IEnumerator enumerator = (IEnumerator)value;
                        while (enumerator.MoveNext())
                        {
                            Console.WriteLine(enumerator.Current.ToString());
                        }
                    }
                }
            }
0
udirAuthor Commented:
Hi,
Thanks for the reply.
I noticed that the values that i need did't get into the results.
I will give an example of how the AD looks like and what i need to retrieve from it :
DC = internet, DC=Test  ----->
                                   OU = CityName  ----->
                                             CN = 1000  -----> (CN is a property of the OU above which has a value = 10)
                                                       ID = 10    (ID is a property of the CN above which has a value = 10)
1) I need to get all the values of a property of the OU, which is the CN values = 1000 and so on.
2) also all the values of a property of the CN, which is the ID values = (10 and so on).
How can i get it with the Foreach statment?
Thanks
                                           
0
Kate12Commented:
I believe you need to use the DirectorySearcher instead... :

DirectoryEntry entry = new DirectoryEntry("LDAP://....");
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(&(objectClass=ou)(CN=100)(ID=10))";
foreach (SearchResult result in searcher.FindAll())
{
    // Properties for the Entry Result.
    //result.Properties
    // Or Get the Directory For this result.
    DirectoryEntry entryResult = result.GetDirectoryEntry();
}

If you need to tweek the syntax you can have look at http://msdn2.microsoft.com/en-us/library/aa746475.aspx for further info.

HTH
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

udirAuthor Commented:
Hi,
Kate12, I'm desperate   (:   or   :(
i just can't get the values.
I wrote :
------------------------------------------------------------
        DirectoryEntry root = new DirectoryEntry(
          "LDAP://10.1.1.1/DC=Internet,DC=Test",
          "User","Password", AuthenticationTypes.ServerBind);

        DirectorySearcher searcher = new DirectorySearcher(root);
        searcher.Filter = "(&(objectClass=user))";
        SearchResultCollection results;
        results = searcher.FindAll();
               
            foreach (SearchResult result in results)
            {
                ResultPropertyCollection rpc = result.Properties;
                foreach (string key in rpc.PropertyNames)
                {
                    foreach (object values in rpc[key])
                    {
                        switch (key)
                        {
                            case "CN":
                                Console.WriteLine("Value is : {0}" + values);
                                break;
                            case "ID":
                                Console.WriteLine("Value is : {0}" + values);
                                break;
                        }
                    }
                }
            }
But it dosn't work (what i'm missing?????)
pls, can you help with that?????
Thanks in advance  
0
Kate12Commented:
Did you debug this code? What happens when you put a breakpoint at the swich statement? Can you inspect the rpc object and see if there is anythign there? ensure that the 'key' matches with the case statements i think that the 'CN' key may be bound as 'cn'.

Properties at AD are noted as single valued or multi-value,  so a property value is always a collection where when it is a single-valued property only the zero index of the property.Value is populated.

HTH
0
udirAuthor Commented:
Yes sure i debbuged it.
The values i get are on the CN level , and my problem is that i don't know how to " drill down "
one more level to the CN properties and get their values (which one of them is - ID property)
Any Idea? (maybe another Foreach statment inside the switch statment?)
0
Kate12Commented:
Have you looked via adsiviewer or any other Adsi browser to see if there is anything under the CN on that particular Entry? What you are doing seems to be correct about now... you should be getting your values.
A friend has written a good article on this too, have a look at http://www.codeproject.com/dotnet/QueryADwithDotNet.asp.

HTH
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
udirAuthor Commented:
Oh, i think the problem is not as i mantioned:
In the loop i get all the values!!!
So now, i only need to filtered them, in order to get only the values of the ID property!!!
How can i do this??
0
Kate12Commented:
as i mentioned before in the code you got to expande your query:

searcher.Filter = "(&(objectClass=user)(ID=10))";

Also in order to optmize the query you can use the PropertyToLoad property at the searcher:
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("id");

HTH
0
udirAuthor Commented:
OK, let me sumerize my problem for now :
as i wrote above - i do get all the values.
now, i need to get only the values of the ID property (which is a property of CN).
The problem is that in the switch statment the propertyNames refers only to the OU propertyNames
and not to the CN propertyNames and thats why i don't get any values of the ID.
0
Kate12Commented:
I dont understand. the property 'cn' of a DirectoryEntry (the one you are using on your switch statement) is short for "Common Name" like "John Doe". In order to get the OU properties you have to use a different LDAP path pointing to your OU e than re-write the query accordingly.
0
udirAuthor Commented:
I also added
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("id");
and
searcher.Filter = "(&(objectClass=user))";
but the switch statment get only the values of the CN properties but not the values of the ID property
0
Kate12Commented:
Are you using AdsiViewer?
0
udirAuthor Commented:
OK, lets leave the cn (because i succeed to get the values).
how do i get the ID values?
0
Kate12Commented:
AdsiViewer will allow you to browse your LDAP provider so you can verify the appropiate path and also will show you the property values (if any) and will also point out if they are single-valued or multi-valued.
0
udirAuthor Commented:
i'm using ADSI Edit
0
udirAuthor Commented:
and the tree is like i wrote before:
DC = internet, DC=Test  ----->
                                   OU = CityName  ----->
                                             CN = 1000  -----> (CN is a property of the OU above which has a value = 10)
                                                       ID = 10    (ID is a property of the CN above which has a value = 10)
so i need all the ID's values.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.