Solved

Query Active Directory From VB.NET

Posted on 2014-03-12
4
1,743 Views
Last Modified: 2016-02-02
I need to pull a list of usernames from active directory that have a particular value in a custom attribute.

I have the AD query which was built in the Active Directory Users and Computers tool and works just fine:

(&(objectCategory=user)(objectClass=user)(extensionAttribute1=myCustomValue))

how can I run this in VB.NET and add each resulting user to a drop-down list?

I hope that makes sense :)
0
Comment
Question by:BrianFord
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39925894
Firstly you'll need to add a reference to the System.DirectoryServices assembly, and an Import statement like:
Imports System.DirectoryServices

Open in new window

Then you can use something like (where ddl is the name of the dropdownlist):
        Dim search As New DirectorySearcher
        search.Filter = "(&(objectCategory=user)(objectClass=user)(extensionAttribute1=myCustomValue))"
        search.PropertiesToLoad.AddRange(
            New String() {"SAMAccountName", "givenname", "sn"}
            )

        Dim results As SearchResultCollection = search.FindAll()
        For Each result As SearchResult In results
            ddl.Items.Add(result.Properties("givenname")(0))
        Next

Open in new window

You may need to tweak it for security, etc, depeding on how your AD is configured.
0
 

Author Comment

by:BrianFord
ID: 39925961
thank you, I will try this out shortly
0
 

Author Closing Comment

by:BrianFord
ID: 39926036
Works like a charm

thanks again
0
 

Expert Comment

by:hamzajosh
ID: 41445349
I tried the above code and get a message saying "The specified domain either does not exist or could not be contacted." on the search.findall() code
Where do i specify the domain to search for in the code. I am trying it on my standalone laptop for now without connecting to any domain FYI.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

685 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