I've seen many companies that have someone maintain a document that acts a phone directory for the company (be it Word/Excel/PDF/web page). I always think that this is a slight waste of time, as we already have to maintain a user database (Usually Active Directory), which has attributes for office phone numbers, mobile numbers, fax numbers, email addresses, job titles, and even favourite drink.

If these are all populated and kept updated with information changes, we can easily automatically generate an 'always-current' phone directory with a little bit of scripting.

The first thing you'll want to decide upon, is exactly what data/attributes you'd like to list in the directory. In my example, I'll be listing givenName, sn, title, mail, telephonenumber, mobile and sAMAccountName. We can use any attributes from an Active Directory user object – You can view these by starting the 'Active Directory Users & Computers' MMC, ensuring 'Advanced Features' is enabled on the view menu, then opening the properties windows for a user, and moving to the attribute editor tab – You will then be able to see each attribute, and its value.

Active Directory user attributes

Active Directory user attributes

The next step is to find a web server on your network running IIS (It is possible to do it via Apache/PHP on Linux connecting via LDAP, but this example is using Classic ASP and VBScript.)

We will need to ensure that the role feature of 'ASP' is installed on your server – otherwise IIS will give us a MIME error instead of loading our page.

ASP role feature

ASP role feature

Create a new file in your web server’s root folder (Usually C:\Inetpub\wwwroot), called directory.asp, with the following content:

<%@ Language=VBScript  %>
<% response.Buffer = True %>
<title>Company directory</title>
<h1>Company Directory</h1>
' Define the AD OU that contains our users
usersOU = "LDAP://OU=Users,DC=Domain,DC=local"
' Make AD connection and run query
Set objCon = Server.CreateObject("ADODB.Connection")
objCon.provider ="ADsDSOObject"
objCon.Properties("User ID") = "DOMAIN\user"
objCon.Properties("Password") = "Pa$5w0rD!"
objCon.Properties("Encrypt Password") = TRUE
objCon.open "Active Directory Provider"
Set objCom = CreateObject("ADODB.Command")
Set objCom.ActiveConnection = objCon
objCom.CommandText ="select givenName,sn,title,mail,telephonenumber,mobile,sAMAccountName FROM '"+ usersOU +"' where sAMAccountname='*' ORDER by sAMAccountname"
Set objRS = objCom.Execute
' Loop over returned recordset and output HTML
Response.Write "<table>" + vbCrLf
Do While Not objRS.EOF Or objRS.BOF
	Response.Write "  <tr>"
	Response.Write "<td>" + objRS("givenName") + "</td>"
	Response.Write "<td>" + objRS("sn") + "</td>"
	Response.Write "<td>" + objRS("title") + "</td>"
	Response.Write "<td>" + objRS("mail") + "</td>"
	Response.Write "<td>" + objRS("telephonenumber") + "</td>"
	Response.Write "<td>" + objRS("mobile") + "</td>"
	Response.Write "<td>" + objRS("sAMAccountName") + "</td>"
	Response.Write "</tr>" + vbCrLf
Response.Write "</table>"
' Clean up
Set objRS = Nothing
Set objCon = Nothing
Set objCom = Nothing

There are a few parts in the above code that will need editing to suit your environment, these being the OU containing your users on line 10, then a username and password with read access to AD on lines 14 & 15.

You can also change the attributes I have decided to use – this will need doing in two places, firstly on the Active Directory query on line 25, then again in the HTML output section in lines 33-39.

The HTML I have used is very basic, but if you've got some artistic flare or have some web designers in your company, I'm sure you'll be able to create something that not only functions well, but looks great too!

I've used Classic ASP for my example, as we can do everything quickly in one file without worrying about Visual Studio. However, if you're comfortable with Visual Studio and ASP.NET, you can achieve the same result using the System.DirectoryServices classes.