Posted By: Robert Shelton, Jr. | Nov 2nd, 2005 @ 6:56 PM | 84,162 Views | 7 Comments

Federal Developer Evangelist, Robert Shelton, takes you through a 12 minute walkthrough/demonstration of how to search Active Directory for users, groups, and other AD Objects.  This demonstration is using the DirectoryServices namespace of the .NET framework.  The demonstration is using Visual Studio 2005, but the code will also work as written for Visual Studio 2003.




You can find the code at my blog: http://sheltonblog.com 


My other AD Screencasts:

- Adding user to AD with .NET

http://channel9.msdn.com/Showpost.aspx?postid=130700
- Adding groups and users to groups with .NET

http://channel9.msdn.com/Showpost.aspx?postid=132400

- AD Searchfilter (Querying) Syntax:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/search_filter_syntax.asp

 - List of SearchScope options:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdirectoryservicesdirectorysearcherclasssearchscopetopic.asp


~ Robert Shelton

Media Downloads:
Rating:
0
0
Robert,

I'm a VIsual Basic developer. Do you have this project available in Visual Basic code?

Thanks,
David Carpenter

I couldn't find the VB-Code in the net, so I just ported it myself:

' If you want to search in a specific path, here's the right spot.
' Just insert the path into "As New DirectoryEntry("LDAP://OU=Accounting,DC=World,DC=com")"
Dim Entry As New DirectoryEntry
Dim Searcher As New DirectorySearcher(Entry)
Dim AdObj As SearchResult

Searcher.SearchScope = SearchScope.Subtree

Searcher.Filter() = "(ObjectClass=user)"

For Each AdObj In Searcher.FindAll
Label1.Text = Label1.Text & "CN=" & AdObj.Properties("CN").Item(0) & " | Path=" & AdObj.Path & "<br>"
Next

I coded it with ASP.net for a webapplication.
But the App does exactely the same as the first example.
I hope you can use it.

hillbillybob77
hillbillybob77
Redneck
First of all...great work. I had been searching for a couple of days for exactly what your screencast showed. I am having some problems though.

Instead of the common name, I would like to display the users logon name. I have been messing around, I got a look at the target's AD schema, and have found some promising attributes to plug in. However, If I try to use and attribute that is not "mandatory" I get a "ArgumentOutOfRangeException"

Here is my code (almost exact to yours)
DirectoryEntry adDirecEntry = new DirectoryEntry();

            DirectorySearcher adDirecSearch = new DirectorySearcher(adDirecEntry);

            //set the search scope
            adDirecSearch.SearchScope = SearchScope.Subtree;

            //Set the filter. For this example we will be looking at all users
            adDirecSearch.Filter = "(ObjectClass=user)";

            //Execute the search and iterate through the result
            //Write results to lboAD for display
           
            foreach (SearchResult adObject in adDirecSearch.FindAll())
            {
                lboAD.Items.Add(adObject.Properties["uid"][0]);
                lboAD.Items.Add(adObject.Path);
            }

It is at "lboAD.Items.Add(adObject.Properties["uid"][0]);" that I get the exception. At first I thought it was maybe something else, I dunno what....but it will only display results when I use a mandatory attribute, no matter the ObjectClass.

Is it something to do with my target's AD schema? Is it something I am doing wrong?

Thanks, and again, great work!!

Is there a way to view video in full screen?

Try

adObject.Properties["SAMAccountName"][0]
it's great code,
but i'm using visual studio 2003 & when i run the code it gives me "The specified domain either does not exist or could not be contacted "
so please advice
thanks
Microsoft Communities