I was asked to make a Phone search/sort webpage for my company using the information in active directory. Not a problem. I am having a problem though with my code where I try to sort on more than one part of the display that it returns no results. If I only use one part to sort on it works just fine but with both location and last name it returns no results.
The code follows and this page is just called from another page and in this case with no name passed to it.
Wasn't sure whether to post this here or in ASP forum but I think it is an ADO VBscript problem. If anybody is an ADO expert the help would be greatly appreciated.
Thanks in advance,
John Woodraska
The code follows and this page is just called from another page and in this case with no name passed to it.
Wasn't sure whether to post this here or in ASP forum but I think it is an ADO VBscript problem. If anybody is an ADO expert the help would be greatly appreciated.
Code:
<%@ Language=VBScript %>
<HTML>
<HEAD>
<TITLE>Phone Number Search Results</TITLE>
<LINK rel="stylesheet" type="text/css" href="styles.css">
</HEAD>
<BODY>
<h1>Phone Number Search Results</h1>
<P>Search results for "<%=Request.QueryString ("Name")%>"</P>
<P><A href="default.asp">Search again</A></P>
<TABLE class=ResultsTable>
<THEAD>
<TR>
<TH>Name</TH>
<TH>Phone Number</TH>
<TH>Department</TH>
<TH>Location</TH></TR>
</THEAD>
<TBODY>
<%
' Build the query string
' First, need to discover the local global catalog server
Set adsRootDSE = GetObject("GC://RootDSE")
' Form a ADsPath string to the DN of the root of the
' Active Directory forest
strADsPath = "GC://" & adsRootDSE.Get("rootDomainNamingContext")
' Wrap the ADsPath with angle brackets to form the base string
strBase = "<" & strADsPath & ">"
' Release the ADSI object, no longer needed
Set adsRootDSE = Nothing
' Specify the LDAP filter
' First, indicate the category of objects to be searched
' (all people, not just users)
strObjects = "(objectCategory=person)"
' Get the name to search for from the URL
strPerson = Request.QueryString("Name")
' If the given name is blank, then filter on all people
If (strPerson = "") Then
strName = "(sn=*)"
Else
strName = "(sn=" & strPerson & "*)"
End If
' Add the two filters together
strFilter = "(&" & strObjects & strName & ")"
' Set the attributes for the recordset to contain
' We're interested in the common name, telephone number, surname, given name, dept, and location
strAttributes = "cn,telephoneNumber,sn,givenname,department,l"
' Specify the scope (base, onelevel, subtree)
strScope = "subtree"
' Create ADO connection using the ADSI OLE DB provider
Set adoConnection = Server.CreateObject ("ADODB.Connection")
adoConnection.Open "Provider=ADsDSOObject;"
' Create ADO commmand object and associate with the connection
Set adoCommand = Server.CreateObject ("ADODB.Command")
adoCommand.ActiveConnection = adoConnection
' Create the command string using the four parts
adoCommand.CommandText = strBase & ";" & strFilter & ";" & _
strAttributes & ";" & strScope
' Set the number of records in the recordset logical page
adoCommand.Properties("Page Size") = 20
' Set the maximum result size
adoCommand.Properties("Size Limit") = 20
' Sort the results based on the l (location) and sn (Surname) attributes
adoCommand.Properties("Sort On") = "l, sn"
' Execute the query for the user in the directory
Set adoRecordSet = adoCommand.Execute
If adoRecordSet.EOF Then
Response.Write "</TBODY><THEAD><TH>No names found</TH></THEAD>"
Else
' Loop through all the returned records
While Not adoRecordSet.EOF
' Display the row using the selected fields
Response.Write "<TR>"
Response.Write "<TD>" & adoRecordSet.Fields("sn") & "," & _
adoRecordset.Fields("givenname") & "</TD>"
' Check to see if telephone number field is null
If IsNull( adoRecordSet.Fields("telephoneNumber") ) Then
Response.Write "<TD>(number not listed)</TD>"
Else
' Retrieve the telephone number and add to the display line
Response.Write "<TD>" & _
adoRecordSet.Fields("telephoneNumber") & "</TD>"
End If
'Retriece department and location and add to the display line
Response.Write "<TD>" & adoRecordset.Fields("department") & "</TD>"
Response.Write "<TD>" & adoRecordset.Fields("l") & "</TD>"
' End the row
Response.Write "</TR>"
' Advance to the next record
adoRecordSet.MoveNext
Wend
End If
' Close the ADO connection
adoConnection.Close
%>
</TBODY>
</TABLE>
</BODY>
</HTML>
Thanks in advance,
John Woodraska