Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Query LDAP and display your domain's Global Address List using VB.NET

0.00/5 (No votes)
14 Nov 2012 1  
A quick run through of how to quickly query LDAP and how to use the resutls of that query.

Introduction

A short while ago I wanted to quickly query LDAP to throw together a simple rendering of my domains Global Address List for non-domain users.  Unlike standard T-SQL and Oracle datasources, I found that there was not a lot of information that showed a simple way to query the data and once I had it I needed a simple way to display it and binding it to a traditional Gridview seemed out of the questions given the unconventional source of this data.  What I aim to do with this article is give you a simple page load event that you can place into a blank asp template and immediately see results. Of course this is not just a code sample, I want to explain what each portion does so that you can customize it to your needs as well.

Before you begin

This article assumes that you are running the ASP content from a PC/Server via IIS that is a member of a domain.  We are not going to specify credentials or FQDN so these values will be retrieved using your current credentials and systems domain membership.  Also on your code behind if when you import "System.DirectoryServices" if you have issues you may need to add a web reference to it, which is easy because it should be listed on the Net tab when you select Add Reference from your project.

Let's get started

  1. Add a simple header row to your HTML table. This is done with the Response.Write command as is the rest of the table that we are going to create. So, in your Page_Load Event for your code behind, paste the following line to write the basic HTML table header row:
  2. Response.Write("<table border=1 bordercolor=#000000><tr> " & _
            "<td bgcolor=#ffff99 width=190px align=center>Employee Name</td>" & _
            "<td bgcolor=#ffff99 width=150px align=center>Phone</td>" & _
            "<td bgcolor=#ffff99 width=150px align=center>Mobile</td>" & _
            "<td bgcolor=#ffff99 width=250px align=center>E-mail Address</td></tr>")
  3. Next declare a variable that will be used for referencing the DirectorySearcher class properties in the System.DirectoryServices namespace.
  4. Dim objsearch As DirectorySearcher = New DirectorySearcher
  5. Next declare a string and fill it with the SearchRoot Path property found in the DirectorySearcher class.
  6. Dim strrootdse As String = objsearch.SearchRoot.Path
  7. Now using the objsearch variable we created earlier we will need to set some properties and values before we initiate the search of AD. This block filters the search to user objects, defines the scope of the search and specifies the return of “common name” object properties in the search. 
  8. See the object reference link at the bottom for more info on AD object properties

    objsearch.Filter = "(& (mailnickname=*)(objectClass=user))"    
    objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree    
    objsearch.PropertiesToLoad.Add("cn")
  9. Next with this block we specify that we want to retrieve only the names of attributes that have assigned values as well as define a sort property and direction.
  10. objsearch.PropertyNamesOnly = True
    objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
    objsearch.Sort.PropertyName = "cn"
  11. Now we define a collection variable of type SearchResultCollection and call the FindAll method of the DirectorySearcher class. This of course executes the query and places the results into this variable.
  12. Dim colresults As SearchResultCollection = objsearch.FindAll
  13. Finally using a For Each loop we will run through the items in the collection and use the Response.Write method to write the contents of each row into our HTML table.
  14. For Each objresult As SearchResult In colresults
          Response.Write("<tr><td>" & objresult.GetDirectoryEntry.Properties("cn").Value & _
          "</td><td>" & objresult.GetDirectoryEntry.Properties("telephoneNumber").Value & _
          "</td><td>" & objresult.GetDirectoryEntry.Properties("mobile").Value & _
          "</td><td><a href='mailto:" & objresult.GetDirectoryEntry.Properties("mail").Value & "'>" & _
          objresult.GetDirectoryEntry.Properties("mail").Value & "</a></td></tr>")
    Next
  15. Lastly we close out the process by writing the closing tag to the HTML table.
  16. Response.Write("</table>")

Because this is all in the Page Load Event simply access the page in IIS or start debugging in VS/VWD and you should be presented with a quick and easy listing of AD properties.

End Result

My hope is that you can see how you can quickly incorporate this into your own project whether it involves validating a domain account or displaying domain member data. Here is the completed page load event.

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Response.Write("<table border=1 bordercolor=#000000><tr> " & _
    "<td bgcolor=#ffff99 width=190px align=center>Employee Name</td>" & _
    "<td bgcolor=#ffff99 width=150px align=center>Phone</td>" & _
    "<td bgcolor=#ffff99 width=150px align=center>Mobile</td>" & _
    "<td bgcolor=#ffff99 width=250px align=center>E-mail Address</td></tr>")
    Dim objsearch As DirectorySearcher = New DirectorySearcher
    Dim strrootdse As String = objsearch.SearchRoot.Path
    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"
    Dim colresults As SearchResultCollection = objsearch.FindAll
    For Each objresult As SearchResult In colresults
        Response.Write("<tr><td>" & objresult.GetDirectoryEntry.Properties("cn").Value & _
             "</td><td>" & objresult.GetDirectoryEntry.Properties("telephoneNumber").Value & _
             "</td><td>" & objresult.GetDirectoryEntry.Properties("mobile").Value & _
             "</td><td><a href='mailto:" & _
             objresult.GetDirectoryEntry.Properties("mail").Value & "'>" & _
             objresult.GetDirectoryEntry.Properties("mail").Value & _
             "</a></td></tr>")
    Next
    Response.Write("</table>")
End Sub

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here