Solved

vb.net ldap query departmentnumber not working

Posted on 2014-10-29
5
148 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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 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

9 Experts available now in Live!

Get 1:1 Help Now