Solved

vb.net ldap query departmentnumber not working

Posted on 2014-10-29
5
143 Views
Last Modified: 2014-10-30
I am attempting to write a list of users that are assigned to a specific departmentnumber in LDAP, which i know should be a list of about 100.  The below code only pulls back one member (which last name starts with T so in my mind it seems like its only returning the last value):

Dim userIds As IEnumerable(Of String) = {"7871"}
        For Each i As String In userIds
            Dim de As New DirectoryEntry("LDAP://test.net:389/DC=test,DC=net")
            Dim LdapFilter As String = "(departmentNumber=" & i & ")"
            Dim searcher As New DirectorySearcher(de, LdapFilter)
            Dim result As SearchResult = searcher.FindOne()
            Dim res As SearchResultCollection = searcher.FindAll()
            Dim item As ListViewItem = ListView1.Items.Add(i)
            item.SubItems.Add(result.Properties("givenName")(0).ToString())
            item.SubItems.Add(result.Properties("cn")(0).ToString())
            item.SubItems.Add(result.Properties("userPrincipalName")(0).ToString())
                next

Open in new window

0
Comment
Question by:derek7467
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40411203
You are using FindOne method which returns you a single user. You need to looping through your res variable as that's a collection of search results.
0
 

Author Comment

by:derek7467
ID: 40411374
How would i do that?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 40412089
Try

Dim userIds As IEnumerable(Of String) = {"7871"}
        For Each i As String In userIds
            Dim de As New DirectoryEntry("LDAP://test.net:389/DC=test,DC=net")
            Dim LdapFilter As String = "(departmentNumber=" & i & ")"
            Dim searcher As New DirectorySearcher(de, LdapFilter)
            Dim result As SearchResult
            Dim res As SearchResultCollection = searcher.FindAll()
          For Each Result In Res
            Dim item As ListViewItem = ListView1.Items.Add(i)
            item.SubItems.Add(result.Properties("givenName")(0).ToString())
            item.SubItems.Add(result.Properties("cn")(0).ToString())
            item.SubItems.Add(result.Properties("userPrincipalName")(0).ToString())
          Next
      next

Open in new window

0
 

Author Closing Comment

by:derek7467
ID: 40413020
worked perfect, thanks!

I dont like to just take the work and not understand so it looks like you cycle through the department code pull everyone back and then for each entry pulled back you cycle through again and pull properties for each, do i have that right?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40414723
Yes that's correct.
0

Join & Write a Comment

Companies that have implemented Microsoft’s Active Directory need to ensure that the Active Directory is configured and operating properly. If there are issues found and not resolved, it eventually leads the components to fail or stop working and fi…
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.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now