Solved

Retrieve Data from Active Directory...

Posted on 2007-03-19
17
1,048 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
ID: 18753006
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
ID: 18754467
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
ID: 18754680
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
ID: 18755078
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
ID: 18755162
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
ID: 18755212
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
ID: 18755254
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
ID: 18755316
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 3

Expert Comment

by:Kate12
ID: 18755330
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
ID: 18755379
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
ID: 18755445
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
ID: 18755446
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
ID: 18755452
Are you using AdsiViewer?
0
 
LVL 1

Author Comment

by:udir
ID: 18755459
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
ID: 18755460
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
ID: 18755465
i'm using ADSI Edit
0
 
LVL 1

Author Comment

by:udir
ID: 18755515
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

920 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

16 Experts available now in Live!

Get 1:1 Help Now