The System.DirectoryServices namespace provides easy access to Active Directory from managed code. The namespace contains two component classes, DirectoryEntry and DirectorySearcher, which use the Active Directory Services Interfaces (ADSI) technology. ADSI is the set of interfaces that Microsoft provides as a flexible tool for working with a variety of network providers.
This is a how-to article to access the GAL from ASP.NET, using the DropDownList ASP.NET server control as the UI. To accomplish this the article will illustrate by utilizing ADSI technology from a C# assembly returning an ArralList in order to bind to the DropDownList.
1) Open Microsoft Visual Studio .NET.
2) Click New Project. Select the Visual C# Projects Item in the Project Types panel on the left. Select Class Library in the Templates panel on the right. Name the project 'gal' or any name you choose in the Name text box and state the location of the project in the Location text box. And click OK to complete the project description.

3) In the Solution explorer right-click the References item and click Add Reference in the pop-up menu. The Add Reference dialog will be displayed. Select the .NET tab and highlight the System.DirectoryServices.dll, then click the Select button on the right to accept and click OK to add the reference to the Solution.

You will now see the added reference to the project.

4) Add to the using declaration section at the top just below the 'using System;' declaration the following two lines.
- using System.DirectoryServices;
- using System.Collections;

These using declarations are needed in order to access the classes of both namespaces, the DirectoryServices namespace to create the DirectorySearcher and DirectoryEntry objects, and the Collections namespace to create the ArrayList object.
You may want to click the link below as a reference to view the methods and properties in this namespace while building the solution.
Note the Default Global Address List Properties from the Exchange Server below. Its default Filter rules are shown.


Note the 'Users' property in the Canonical name of object definition below. We will use this property in our filter query.
(objectClass=user)

In this example I will shorten the filter rules to the following for simplicity sake:
(& (mailnickname=*)(objectClass=user))
shown below in the code.
The (mailnickname=*) filter applies the filter to every object that has a mailNickname attribute (that is, every object that is mail-enabled). The '&' is the equivalent to 'And' in T-Sql syntax. Exchange filter rules' order are not as intuitive as T-Sql as one can see. So the filter in the code means every object that is mail-enabled and every object that is defined as user.
5) Copy and Paste the following code in the class below the default constructor.
- public ArrayList returngal()
- {
- DirectorySearcher objsearch = new DirectorySearcher();
- string strrootdse = objsearch.SearchRoot.Path;
- DirectoryEntry objdirentry = new DirectoryEntry(strrootdse);
- objsearch.Filter = "(& (mailnickname=*)(objectClass=user))";
- objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree;
- objsearch.PropertiesToLoad.Add("cn");
- objsearch.PropertyNamesOnly = true;
- objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
- objsearch.Sort.PropertyName = "cn";
- SearchResultCollection colresults = objsearch.FindAll();
- ArrayList arrGal = new ArrayList();
- foreach (SearchResult objresult in colresults)
- {
- arrGal.Add(objresult.GetDirectoryEntry().Properties["cn"].Value);
- }
- objsearch.Dispose();
- return arrGal;
- }

6) Click on the 'Build' menu item, then click on 'Build Solution' in the pop-up menu.

You will see the following successful build message in the Output/Build pane.

7) Click on the 'File' menu item, then click on 'Save gal.sln' in the pop-up menu to save the project solution.

Now we have a .NET assembly ready to use in our web page.
8) On the chosen web server create a new folder, name it 'gal', under the wwwroot directory.






Cristian CapanniniPosted Sep 25, 2012, 10:57 AM
there are no image!!!
farzaneh ansaryPosted Mar 10, 2008, 7:31 AM
plz send for me source cod eof this articlel [email protected]
farzaneh ansaryPosted Mar 10, 2008, 7:28 AM
hi plz send for me source code of this article
Vicky MinhasPosted Dec 6, 2007, 4:58 AM
thanks for a great article. This article has been a great help to me. Can you please tell ne how to get email id along eith names. Any help would be greatly appreciated. Vicky
Stacie MellPosted Oct 31, 2007, 1:06 PM
I received the following errors after I posted the code from Step 5 and clicked Build Solution: "Expected class, delegate, enum, interface or struct" error on ArrayList, DirectorySearcher, DirectoryEntry) "Identifier expected" error on ["cn"] "A namespace does not directly contain members such as fields or methods" error on objsearch "Type or namespace definition, or end-of-file expected" on the last } in my code.
sadiqPosted Oct 29, 2007, 3:09 AM
I want to list all the departments in the organisation and upon selecting the department i want list all the users in the selected department. could u please provide the solution.
Gonzalo BrusellaeditedPosted May 5, 2006, 9:24 AMEdited Sep 3, 2006, 4:32 PM
On the sample, the you show ALL the gal contents (even the hidden ones). You might want (as i did) to show only the "visible" items. In order to do to such things, you should change the filtering condition to: objsearch.Filter = "(&(objectclass=user)(mailNickname=*)(!msExchHideFromAddressLists=*))"; This will only show the visible contacts of the GAL.