Solved

Retrieve Data from Active Directory...

Posted on 2007-03-19
17
1,041 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:udir
  • 9
  • 8
17 Comments
 
LVL 3

Expert Comment

by:Kate12
Comment Utility
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
 
LVL 1

Author Comment

by:udir
Comment Utility
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
 
LVL 3

Expert Comment

by:Kate12
Comment Utility
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
 
LVL 1

Author Comment

by:udir
Comment Utility
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
 
LVL 3

Expert Comment

by:Kate12
Comment Utility
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
 
LVL 1

Author Comment

by:udir
Comment Utility
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
 
LVL 3

Accepted Solution

by:
Kate12 earned 500 total points
Comment Utility
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
 
LVL 1

Author Comment

by:udir
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:Kate12
Comment Utility
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
 
LVL 1

Author Comment

by:udir
Comment Utility
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
 
LVL 3

Expert Comment

by:Kate12
Comment Utility
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
 
LVL 1

Author Comment

by:udir
Comment Utility
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
 
LVL 3

Expert Comment

by:Kate12
Comment Utility
Are you using AdsiViewer?
0
 
LVL 1

Author Comment

by:udir
Comment Utility
OK, lets leave the cn (because i succeed to get the values).
how do i get the ID values?
0
 
LVL 3

Expert Comment

by:Kate12
Comment Utility
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
 
LVL 1

Author Comment

by:udir
Comment Utility
i'm using ADSI Edit
0
 
LVL 1

Author Comment

by:udir
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Resolve DNS query failed errors for Exchange
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now