Accessing Global Address List via System.DirectoryServices namespace

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.



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 =
strrootdse = objsearch.SearchRoot.Path ;
DirectoryEntry objdirentry =
objsearch.Filter = "(& (mailnickname=*)(objectClass=user))";
objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree;
objsearch.PropertyNamesOnly =
objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
objsearch.Sort.PropertyName = "cn";
SearchResultCollection colresults = objsearch.FindAll() ;
ArrayList arrGal =
foreach(SearchResult objresult in
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.


9) Create a virtual directory on the chosen web server using the Virtual Directory Creation Wizard to input the Alias, name it 'gal', the Directory, choose the newly created folder 'gal', and the Access Permissions(check the Read and Run Scripts at least) properties.



10) Open upon notepad and copy and paste the following code into the editor. Now save the file as 'gal.aspx' into the gal directory on the web server.

 <%@ Page Language="c#" Debug="true" %>
<script language="c#" runat="server">
void Page_Load(object sender, EventArgs e)
gal.Class1 s = new gal.Class1();
gal.DataSource = s.returngal();
gal.Items.Insert( 0, new ListItem("GAL") );
<form id="Form" runat="server">
<ASP:DropDownList id="gal" runat="server" />

11) Copy and paste the following code into the editor. Now save the file as 'web.config' into the gal directory on the web server.

<?xml version="1.0" encoding="utf-8" ?>


<customErrors mode="Off" />

<authentication mode="None" />


12) Create a new folder, name it 'bin', under the gal directory of the web server. Now copy the 'gal.dll' from the Debug folder which is under the bin folder in the project folder 'gal' and paste this gal.dll into the 'bin' folder of the web server.

Copy from the folder below.


Paste it into the folder below.


13) Type the path to the gal.aspx in the browser and see results below.


If error messages appear, there may be problems with ASP.NET Base Account
See the following article at;en-us;329986

The web.config file may need some changes and the security of the virtual directory may need some adjusting. The article above should lend some light on the issue.

A possible solution could be the following:

Edit the web.config file.

<authentication mode="Windows" />
<identity impersonate="true" />

And change the virtual directory security setting. Clear the Anonymous Authentication check box and check the Integrated Windows Authentication property.

There may be an issue with the performance of the page on request due to the size of the GAL. You may have to implement some caching into your gal.aspx page similar to the following.

ArrayList arrGal = new ArrayList();
arrGal = (ArrayList)Cache["arrGal"];
if ( arrGal == null
gal.Class1 s =
arrGal = s.returngal();
Cache["arrGal"] = arrGal ;
gal.DataSource = arrGal ;
gal.Items.Insert( 0,
new ListItem("GAL") );

This will greatly affect the page's performance and enhance the user's experience.

You may want to use a cache dependency like the following:

Cache.Insert("arrGal ", arrGal, null, DateTime.Now.AddMinutes(2), NoSlidingExpiration);

There may be an issue with the number of objects returned from the objsearch.FindAll() method. By default it will only return a maximum of 1000 objects in the SearchResultCollection. So you might have to do a little coding here. A possible solution is to have more than one implementation of FindAll() in the code, using a property to limit the searches eventually grabbing all the objects. I will use the 'cn' property to illustrate. The cn property is short for the common name attribute.

objsearch.Filter = "(& (mailnickname=*)(objectClass=user) (cn<=M))";

for one search and

objsearch.Filter = "(& (mailnickname=*)(objectClass=user) (cn>=M))";

for the second search, effectively using two, or more if needed, searches to select all the objects resulting in two or more SearchResultCollection objects to loop through.

foreach(SearchResult objresult in colresults)
foreach(SearchResult objresult2 in

A url link which may be helpful to access search attributes is:

Similar Articles