Solved

vb.net ldap query departmentnumber not working

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 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