Solved

Query Active Directory From VB.NET

Posted on 2014-03-12
4
1,586 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
Comment Utility
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
Comment Utility
thank you, I will try this out shortly
0
 

Author Closing Comment

by:BrianFord
Comment Utility
Works like a charm

thanks again
0
 

Expert Comment

by:hamzajosh
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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 …

744 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

15 Experts available now in Live!

Get 1:1 Help Now