Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort AD ldap results A-Z in ASP

Status
Not open for further replies.

GregW

MIS
Oct 10, 1998
5
AU
I use the following asp code to return staff details from AD to create a searchable web based address book. It works well but the results are displayed in a random order. could one of you code gurus please provide the code to sort the results alphabetically?

many thanks. Greg.

'********************************************************
' function: find a user in Exchange
' author: Christian Kiefer
' email: christian.kiefer@bsp.de
'********************************************************


'*** you only have to change this variable:
'*** the rest will work automaticly

strServerName = "Exchange01"



'******************************************
bolSearch = false

strLastname = Request("lastname")
strName = Request("name")

if strName<>"" or strLastname<>"" then
bolSearch = true
end if

%>
<HTML>
<head>
<title>Suche Exchange</title>
<style>
<!--
BODY {font-family:Arial,Helvetica; font-size:10pt;}
TD {font-family:Arial,Helvetica; font-size:9pt;}
//-->
</style>
</head>
<BODY>
<CENTER><H3>Search in Exchange</H3><CENTER>

&nbsp;

<%
set oConn = CreateObject("ADODB.Connection")
set oCommand = CreateObject("ADODB.Command")

oConn.Provider = "ADsDSOObject"
oConn.Open "Ads Provider"

set oCommand.ActiveConnection = oConn

if bolSearch then

'***put query string together
strQuery = "<LDAP://" & strServername & ">;(&(objectClass=*)(sn=" & strLastname & "*)(givenname=" & strName & "*));sn, department, givenname,cn,telephoneNumber,mail;subtree"

oCommand.CommandText = strQuery
set oRS = oCommand.Execute

'***display table only if hits or search criteria are given
if bolSearch and not oRS.eof then
%>

<div align="center">
<table cellpadding="0" border="0" cellspacing="0">
<tr bgcolor="#C0C0C0">
<td height="20"><b>Name&nbsp;</b></td>
<td height="20"><b>Department&nbsp;</b></td>
<td height="20"><b>Telefon&nbsp;</b></td>
<td height="20"><b>Email&nbsp;</b></td>
</tr>
<%While not ors.eof%>
<tr>
<td><%=oRS.Fields("sn")%>, <%=oRS.Fields("givenname")%>&nbsp;</td>
<td><%=oRS.Fields("department")%>&nbsp;</td>
<td><%=oRS.Fields("telephoneNumber")%>&nbsp;</td>
<td><a href="mailto:<%=oRS.Fields("mail")%>"><%=oRS.Fields("mail")%>&nbsp;</A></td>
</tr>
<%
oRS.MoveNext
wend %>
<tr bgcolor="#C0C0C0">
<td colspan="4" height="20">&nbsp;</td>
</tr>
</table>
</div>
<%
else
Response.Write "No entry found"
end if

end if
%>


<br>
<br>
<form method="post" action="search_exchange.asp">
<table border="0" cellpadding="0" cellspacing="1" style="border-collapse: collapse" bordercolor="#111111" >
<tr>
<td colspan="5" bgcolor="#C0C0C0" height="20"><b>&nbsp;Search</b></td>
</tr>
<tr>
<td><b>Lastname&nbsp;&nbsp;</b></td>
<td valign="top"><input type="text" name="lastname" value="<%=strLastname%>" size="20"></td>
<td><span lang="de"><b>&nbsp;&nbsp;</b></span></td>
<td><b>N</b><span lang="de"><b>ame</b></span></td>
<td valign="top">
<p align="center">
<input type="text" name="name" value="<%=strName%>" size="20"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Search" name="start search"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>

</body>
</html>
 
try
Code:
.....
    oCommand.CommandText = strQuery
    set oRS = oCommand.Execute
    [COLOR=blue]oRS.sort "sn  ,givenname"[/color]
    '***display table only if hits or search criteria are given
    if bolSearch and not oRS.eof then
    %>

.....
 
Thanks for the tip.
It's not working in this code though. The response is "Wrong number of arguments or invalid property assignment: 'sort'".
I've also tried with just one filed inthe sort statement, but still no luck.
Any advice?

thanks,

greg.

 
I've tried adding an equals sign so line reads:
oRS.sort = "sn"
Now I receive the error "Current provider does not support the necessary interfaces for sorting or filtering."
This sounds to me like the asp ldap code doesn't support sorting. would it be possibe to put the ldap output into a table then do a sort on it?

thanks,

Greg.
 
I'did some Googing and found a Microsoft tip that states "You cannot use the distinguishedName attribute to sort an LDAP query in Windows 2000 or in Windows Server 2003"


It contains the following code:

szQuery = "<LDAP://billtivpc01.ticehurst.com>;" + _
"(&(objectCategory=group)(&(distinguishedName=*)));" + _
"objectGUID, ou, distinguishedName;subtree"

Set g_oCon = CreateObject("ADODB.Connection")
Set g_oRS = CreateObject("ADODB.Recordset")

g_oCon.Provider = "ADsDSOObject"
g_oCon.Properties("Encrypt Password") = True
g_oCon.Properties("ADSI Flag") = adSecureAuthentication
g_oCon.Open "Active Directory Provider"


'*** The following server-side sort does not return any results.
'Set g_oCmd = CreateObject("ADODB.Command")
'Set g_oCmd.ActiveConnection = g_oCon
'g_oCmd.Properties("Sort On") = "distinguishedname"
'g_oCmd.CommandType = adCmdText
'g_oCmd.CommandText = szQuery
'g_oRS.Open g_oCmd, , adUseClient, adLockReadOnly


'*** Instead, the following client-side sort succeeds.
g_oRS.CursorLocation = adUseClient
g_oRS.Sort = "distinguishedname"
g_oRS.Open szQuery, g_oCon, , , adCmdText


While Not g_oRS.EOF
WScript.Echo g_oRS.Fields("distinguishedName").Value
g_oRS.MoveNext
Wend

WScript.Quit

I don't know enough asp code to be able to alter my original code to include this client-side sort. If anyone cna help me out here I'd really appreciate it.

Thanks,

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top