?
Solved

LDAP question

Posted on 2006-04-04
10
Medium Priority
?
592 Views
Last Modified: 2012-06-21
I am trying to retrieve all the entries from LDAP where Organizational unit is "Test Unit"

Here is the code I use:

Try
            Dim sPath As String = "LDAP://server1/DC=domain1,DC=com"
           
            Dim sDir As New DirectoryEntry(sPath, "admin", "temp")
            Dim sSearcher As New DirectorySearcher(sDir)
            Dim sSearchResultColl As SearchResultCollection
            Dim sSearchResult As SearchResult

            'Build LDAP query
           
             sSearcher.Filter = ("(&(ou=" & key & "))")
               
            sSearchResultColl = sSearcher.FindAll()

Then if I check sSearchResultColl.Count, I get 1 even though there are at least 4 entruies wher OU is "Test Unit"

What am I doing wrong?
0
Comment
Question by:YZlat
  • 5
  • 5
10 Comments
 
LVL 20

Expert Comment

by:ihenry
ID: 16372468
You mean the SearchResultCollection object returns 1 result even though there are 4 entries of OU with that name in the domain?

What happens if you use a query filter like:

sSearcher.Filter = String.Format( "ou={0}", key )
0
 
LVL 35

Author Comment

by:YZlat
ID: 16373315
Nope, still returning 1.

I have the following entries in my LDAP

LDAP://server1/CN=ENT2,OU=Test Unit,DC=domain1,DC=com
LDAP://server1/CN=ENT3,OU=Test Unit,DC=domain1,DC=com
LDAP://server1/CN=ENT4,OU=Test Unit,DC=domain1,DC=com
LDAP://server1/CN=ENT1,OU=Test Unit,DC=domain1,DC=com

and it returns

LDAP://server1/OU=Test Unit,DC=domain1,DC=com

0
 
LVL 35

Author Comment

by:YZlat
ID: 16373491
I think I see where the problem is:

LDAP://server1/CN=ENT2,OU=Test Unit,DC=domain1,DC=com
LDAP://server1/CN=ENT3,OU=Test Unit,DC=domain1,DC=com
LDAP://server1/CN=ENT4,OU=Test Unit,DC=domain1,DC=com
LDAP://server1/CN=ENT1,OU=Test Unit,DC=domain1,DC=com

All the entries above are the same, except CN value. I think my code somehow reads each entry starting from OU=Test Unit, so all the 4 entries seem the same. Is there a workaround, to read each entry completely?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Expert Comment

by:ihenry
ID: 16382016
Apparently, the full DNS names are not showing that the objects are OU (OrganizationalUnit). That could be user objects, group objects, or other AD object types. The objectClass or objectCategory attribute have the information to determine type of AD object. From there, you can construct the correct query filter and use it in your LDAP search.
0
 
LVL 35

Author Comment

by:YZlat
ID: 16382287
I've tried

sSearcher.Filter = ("(&(objectClass=organizationalUnit)(ou=Test Unit))")

but got the same result
0
 
LVL 20

Expert Comment

by:ihenry
ID: 16382819
This "OU=Test Unit,DC=domain1,DC=com" is an organizationalUnit.

and this "CN=ENT2,OU=Test Unit,DC=domain1,DC=com" is *not* an organizationalUnit object.

You can use a LDAP utility to look into the objectClass or objectCategory attribute of the object and see what is the value inside.
0
 
LVL 35

Author Comment

by:YZlat
ID: 16383587
so how would I filter the results on an Organizational unit "Test Unit" or on user "Test User"?

What I want is for my function to return a collection of objects from AD, depending on Organizational Unit or on the User that are passed to the function
0
 
LVL 20

Expert Comment

by:ihenry
ID: 16383792
I might not have explained it to you clearly. I'm under impression that the structure of the OU is like this

-- Test Unit (OrganizationalUnit)
   |-- ENT1  (User or Group or other object type)
   |-- ENT2  (User or Group or other object type)
   |-- ENT3  (User or Group or other object type)
   |-- ENT4  (User or Group or other object type)

To find all objects under an OU you can just point the DirectoryEntry object to the OU's fully DN name, e.g.
Dim sDir As New DirectoryEntry("LDAP://domain.com/ou=test unit,dc=domain,dc=com"/, "admin", "temp")
with this filter: (cn=*)

To find all user objects under an OU, you can use this filter
(&(objectClass=user)(objectCategory=person))

To find all group objects under an OU, you can use this filter
(objectClass=group)
0
 
LVL 35

Author Comment

by:YZlat
ID: 16383999
but the question I have is
how do I find all objects that belong to a particular OU?
0
 
LVL 20

Accepted Solution

by:
ihenry earned 500 total points
ID: 16384085
My answer, as I have posted in my previous post, is to make use your original code in question. But instead pointing the DirectoryEntry object to the root domain, you need to get it pointed to the OU in which the object you're searching resides. And use the filter "(cn=*)" in the DirectorySearcher's Filter property.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

839 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