?
Solved

vb.net ldap query departmentnumber not working

Posted on 2014-10-29
5
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

801 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