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!

Example of ASP LDAP query string? 14

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
Could someone post an example of ASP code used to query LDAP without any proprietary components?

Thanks! Take Care,
Mike
 
Many thanks Zac, works perfectly. I have hacked it up to do the following.

1. Removed the web based email function, using it internally so users will use their email clients for communication.

2. Changed it to default to a listing of all employees on load, instead of a blank page telling them to choose a letter and/or location

3. The main page now shows Full Name, Title, Department, Location, Phone, and Fax. Right frame elimanted so all info above fits. No need for a details page per user now. Instead, the users name is a mailto: link now.

4. Added headings of Full Name, Title, Department, Location, Phone, and Fax above output listing.

Basically, I have it down to two files, phonebook.asp and list.asp

My changes, like Zac's contribution, are free for the asking.
 
I have modified my code so that it requires no changes to work on your system. If you do download a copy from the link I posted above, please take a look at the readme.
 
hi Zac, I tested the phonebook.asp at my win2K IIS. The asp listed A to Z, but when I click on any of the letter, nothing return. In fact an "Error on Page" detected at the bottom task bar with error details 'document.departmentchoice.departmentname[...].checked' is null or not an object.
Please advice. Thanks.
 
The top bar of radio buttons is used to select which department the list of user is filtered with. Are you using that field in you AD? Let me know if your still having problems.
 
JBIGHAM,

I would love to have that code you hacked up. Very nice work zcolton.

tfuhrman@frsd.k12.nj.us
 
I was wondering if your modified version still displays the departments up top?

Ideally for me would be to display the departments up top, but upon choosing them have them auto display all members of that department by display name or last name/ first name, which in my case is their display name.

I'm thinking my desired phonebook lies somewhere between the original and your modified version.

Hopefully you check in soon and can send those files over. Thank you in advance.
 
ToddFuhrman,

It wouldn't take much to change the latest code I have to do what you want. Would you like me to put something together for you?
 
Thanks Zac. I downloaded and put it up at the web. I was able to change a lot of things I wanted to but I did not touch the email component of it.

Can you tell me how to change the code to a mailto: type response. I don't want anonymous emails, we want to click on the email address and have the clients email application open a new mail.

Check out what I have, looks great!

 
Zip up your phonebook directory and email it to me. I can make the changes.
zcolton@burltwpsch.org
 
YGM

I have some different features in my endeavor.

The ability to navigate up the org chart, assuming you have populated your manager field in ADUC. Found on the users details page (click on a user)

Email is setup to use the users email client. Just plain safer this way (spammers, jokers, audit tracking, etc)

Default behavior is to show all users on launch, rather than requiring a letter to be chosen.

An option to revert back to show all users at any time, see * symbol.

Phone # field pulls from the 1st element in the otherTelephone array.

Layout is somewhat different.

Not yet implemented the code to make it more transportable (still have to code server names)
 
jbigham

take a look at my newest code. It doesn't require server or domain names to be hard coded. This exmple just spits out a list of departments. I was using this to help someone else.

Example:

Code:
<%@ Language=VBScript %>
<%
Option Explicit
Dim con,rs,Com,objADsPath,objDomain
%>
<html>
<head>
</head>
<body bgcolor=&quot;#CCCCCC&quot;>
<%
Set objDomain = GetObject (&quot;GC://RootDSE&quot;)
objADsPath = objDomain.Get(&quot;defaultNamingContext&quot;)
Set objDomain = Nothing
Set con = Server.CreateObject(&quot;ADODB.Connection&quot;)
con.provider =&quot;ADsDSOObject&quot;
con.open &quot;Active Directory Provider&quot;
Set Com = CreateObject(&quot;ADODB.Command&quot;)
Set Com.ActiveConnection = con
Com.CommandText =&quot;select department from 'GC://&quot;+objADsPath+&quot;' WHERE department ='*'&quot;
Set rs = Com.Execute

Do While Not rs.EOF Or rs.BOF

Response.Write rs(&quot;department&quot;) & &quot;<BR>&quot;

rs.MoveNext
Loop
rs.Close

con.Close
Set rs = Nothing
Set con = Nothing
%>
</body>
</html>
 
I was thinking about doing something like that... just no time. This is the &quot;fun stuff&quot; that I have to hold off on until other projects are completed.

A variation of that will work for the printable list I wanted to do though, as grouping by department is preferred. I'd just do:

Response.Write rs(&quot;department&quot;) & &quot;<BR>&quot;

<insert loop to list associated departmental employees)

rs.MoveNext

I'll keep you udated as I make changes... I have webspace, guess I should just start posting my sources. Maybe after I weed out our hard-coded company info in there.
 
Quick note, I think you need to loop through that recordset again so that you can extract distinct entries for departments... so you don't have repeating groups.

I see you did this for someone else... so it may not apply for you setup.
 
The code I just posted was for someone who was having problems just getting the department info. This is my actual asp page that will retrieve a complete list of departments and use them as choices for the query.

Code:
<%@ Language=VBScript %>
<%
Option Explicit
Dim t,x,con,rs,Com,objADsPath,objDomain,iLoop,bolFound,strdepartments
strdepartments=&quot;&quot;
%>
<html>
<head>
<script type=&quot;text/javascript&quot;> 

function listlink(a,q) 
{
for (var c = 0; c <= q; c++)
    {if (document.departmentchoice.departmentname[c].checked) b = document.departmentchoice.departmentname[c].value;}
open('list.asp?letter='+a+'&department='+b,'ListFrame');
open('info.asp','InfoFrame');
open('blank.htm','EmailFrame');
} 

function reflist()
{
open('list.asp','ListFrame');
open('blank.htm','InfoFrame');
open('blank.htm','EmailFrame');
}
</script>

<title>Phone List</title>
<style>
.over { background-color: #FFFF66; cursor: hand}
.out { background-color: #CCCCCC}
</style>
</head>
<body bgcolor=&quot;#CCCCCC&quot; topmargin=&quot;0&quot; leftmargin=&quot;0&quot;>
<%
Set objDomain = GetObject (&quot;GC://rootDSE&quot;)
objADsPath = objDomain.Get(&quot;defaultNamingContext&quot;)
Set objDomain = Nothing
Set con = Server.CreateObject(&quot;ADODB.Connection&quot;)
con.provider =&quot;ADsDSOObject&quot;
con.open &quot;Active Directory Provider&quot;
Set Com = CreateObject(&quot;ADODB.Command&quot;)
Set Com.ActiveConnection = con 
Com.CommandText =&quot;select department from 'GC://&quot;+objADsPath+&quot;' WHERE objectCategory='person' AND department='*' ORDER BY department&quot;
Set rs = Com.Execute 

Do While Not rs.EOF 
 dim myarray
 myarray=split(strdepartments,&quot;,&quot;)
 bolFound = False
 For iLoop = LBound(myarray) to UBound(myarray)
  If CStr(myarray(iLoop)) = CStr(rs(&quot;department&quot;)) Then
   bolFound = True
  End If
 Next
 IF bolFound = False Then
  If strdepartments=&quot;&quot; then
   strdepartments=rs(&quot;department&quot;)
   Else
    strdepartments=strdepartments&&quot;,&quot;&rs(&quot;department&quot;)
  End If
 End If
rs.MoveNext
Loop
rs.Close
dim deparray
deparray=split(strdepartments,&quot;,&quot;)
t=UBound(deparray)+1
%>
<form name=&quot;departmentchoice&quot;>
<table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;550&quot;>
<tr><td colspan=&quot;26&quot; valign=&quot;middle&quot; align=&quot;center&quot; height=&quot;20&quot;><p><font face=&quot;Tahoma&quot; size=&quot;1&quot;><b>
  <input type=&quot;radio&quot; onclick=&quot;reflist();&quot; name=&quot;departmentname&quot; value=&quot;*&quot; checked>All
<%For iLoop = LBound(deparray) to UBound(deparray)%>
  <input type=&quot;radio&quot; onclick=&quot;reflist();&quot; name=&quot;departmentname&quot; value=&quot;<%response.write deparray(iLoop)%>&quot;><%response.write deparray(iLoop)%>
<%Next%>
</b></font></p></td> 
</tr>
<tr>
<%For x = 65 to 90%>
 <td onMouseOver=&quot;this.className='over'&quot; onMouseOut=&quot;this.className='out'&quot; class=&quot;out&quot; width=&quot;20&quot; height=&quot;20&quot; valign=&quot;middle&quot; align=&quot;center&quot; onclick=&quot;listlink('<%response.write chr(x)%>','<%response.write t%>');&quot;><a><b><font color=&quot;#000080&quot; face=&quot;Verdana&quot; size=&quot;2&quot;><%response.write chr(x)%></font></b></a></td>
<%Next%>
</tr>
<tr>
 <td valign=&quot;top&quot; colspan=&quot;10&quot;><IFRAME NAME=&quot;ListFrame&quot; FRAMEBORDER=&quot;0&quot; SCROLLING=&quot;AUTO&quot; SRC=&quot;list.asp&quot; width=&quot;223&quot; height=&quot;458&quot;></IFRAME></td>
 <td valign=&quot;top&quot; colspan=&quot;16&quot;><IFRAME NAME=&quot;InfoFrame&quot; FRAMEBORDER=&quot;0&quot; SCROLLING=&quot;AUTO&quot; SRC=&quot;blank.htm&quot; width=&quot;320&quot; height=&quot;125&quot;></IFRAME>
 <IFRAME NAME=&quot;EmailFrame&quot; FRAMEBORDER=&quot;0&quot; SCROLLING=&quot;AUTO&quot; SRC=&quot;blank.htm&quot; width=&quot;320&quot; height=&quot;330&quot;></IFRAME></td>
</tr>
</table>
</form>
</body>
</html>
 
Greetings all,
I'm attempting to implement something close to this on an ASP through FrontPage (with MS back end), and am not sure how to modify it for my situation.

What I'm trying to do is get an ASP to detect the currently logged on user and query AD to display their email address on the screen. I've already got my ASP running a JavaScript that will properly detect and display their username, but this bit with displaying the email address has been more daunting. I'm not a programmer, so I'm trying to piece things into place with examples from other sites. No luck so far.

My concerns are:
1. My method has been to use the (Request.ServerVariables(&quot;LOGON_USER&quot;)) variable to query LDAP and find the email address, but no luck so far.
2. What is the proper URL syntax for an LDAP server? I have a domain controller name to work off of, but have so far been unable to properly connect to it. I'm not sure how to fit this in with the DN.. Still learning how these all interact.
3. My ASP runs a JavaScript to find the username of the user accessing the page. Is it also possible to run a VBScript on the same page?

*phew!* That's enough to get started. Any help is *greatly* appreciated.

Thanks!
Chris
 
This is the best example that I've been able to work with so far. The example was taken from
I believe this example will do what I'm looking for, though the only value I really need returned is the email address (this should return many more values, but if it can work, it can be cut back to just display the email address).

I think the only change needed here is to add the correct LDAP info.. The values in this code are from the example on the link above. Perhaps also change the 'script language' value to VBScript? The final working version of this code will be pasted onto an ASP form page with JavaScript though, so not sure yet how those will work together.

<html>

<head>
<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft FrontPage 5.0&quot;>
<meta name=&quot;ProgId&quot; content=&quot;FrontPage.Editor.Document&quot;>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=windows-1252&quot;>
<title>testsearch</title>

<script language=&quot;JavaScript&quot;></script>

</head>

<body>

<%
on error resume next

strLogonID = Request.Servervariables(&quot;LOGON_USER&quot;)

If InStr(strLogonID, &quot;\&quot;) > 0 Then
strLogonID = LCase(Right(strLogonID, InStr(strLogonID, &quot;\&quot;) - 2))
End If

id = LCase(strLogonID)

Set User = GetObject(&quot;WinNT://scotland/&quot; & id & &quot;&quot;)
Fullname = User.Fullname

'###### Pathway to use to Active Directory ######
testpath = &quot;LDAP://CN=&quot; & FullName & &quot;,OU=\#Scots Users,DC=scotland,DC=co,DC=uk&quot;
set usr = GetObject(testpath)

'###### Flush any cache held ######
usr.GetInfo

'###### Active Directory variables ######
strName = usr.get(&quot;givenName&quot;) '###### Users Christian Name ######
strSurname = usr.get(&quot;sn&quot;) '###### Users Surname ######
strInitials = usr.get(&quot;initials&quot;) '###### Users Initials ######
displayName = usr.get(&quot;displayName&quot;) '###### Full Display Name ######
strAddress = usr.get(&quot;StreetAddress&quot;) '###### Address Information ######
strRoom = usr.get(&quot;PhysicalDeliveryOfficeName&quot;) '###### Room/Area Information ######
secretary = usr.get(&quot;secretary&quot;) '###### Secrectary/Assistant ######
strTitle = usr.get(&quot;title&quot;) '###### Job Title ######
strTelephone = usr.get(&quot;telephoneNumber&quot;) '###### Official Intneral Telephone Number ######
strFax = usr.get(&quot;facsimileTelephoneNumber&quot;) '###### Fax Number ######
mobile = usr.get(&quot;mobile&quot;) '###### Mobile Number ######
telephoneAssistant = usr.get(&quot;telephoneAssistant&quot;) '###### Telephone Assistant ######
strDepartment = usr.get(&quot;department&quot;) '###### Department ######
strCC = usr.get(&quot;ExtensionAttribute1&quot;) '###### COST Centre ######
strBuilding = usr.get(&quot;ExtensionAttribute2&quot;) '###### Building ######
strDivision = usr.get(&quot;ExtensionAttribute3&quot;) '###### Division ######
strBranch = usr.get(&quot;ExtensionAttribute4&quot;) '###### Branch ######
ExtensionAttribute5 = usr.get(&quot;ExtensionAttribute5&quot;) '###### PC Item Number ######
ExtensionAttribute6 = usr.get(&quot;ExtensionAttribute6&quot;) '###### External Telephone Number ######
strGroup = usr.get(&quot;ExtensionAttribute7&quot;) '###### Group ######
othertelephone = usr.get(&quot;othertelephone&quot;) '###### GTN Telephone Number ######
samaccountname = usr.get(&quot;samaccountname&quot;) '###### User ID ######
adspath = usr.get(&quot;adspath&quot;) '###### Pathway to AD? ######
strFullName = usr.get(&quot;cn&quot;) '###### Full Display Name ######
strMail = usr.get(&quot;mail&quot;) '###### E-Mail Address ######
strManager = usr.get(&quot;manager&quot;) '###### Reporting Officer ######
'###### End Active Directory variables ######

set usr = nothing

Response.Write &quot;Firstname = &quot; & strName & &quot;<br />&quot;
Response.Write &quot;Surname = &quot; & strSurname & &quot;<br />&quot;
Response.Write &quot;initials = &quot; & strInitials & &quot;<br />&quot;
Response.Write &quot;displayName = &quot; & displayName & &quot;<br />&quot;
Response.Write &quot;StreetAddress = &quot; & strAddress & &quot;<br />&quot;
Response.Write &quot;PhysicalDeliveryOfficeName = &quot; & strRoom & &quot;<br />&quot;
Response.Write &quot;secretary = &quot; & secretary & &quot;<br />&quot;
Response.Write &quot;title = &quot; & strTitle & &quot;<br />&quot;
Response.Write &quot;telephoneNumber = &quot; & strTelephone & &quot;<br />&quot;
Response.Write &quot;facsimileTelephoneNumber = &quot; & strFax & &quot;<br />&quot;
Response.Write &quot;mobile = &quot; & mobile & &quot;<br />&quot;
Response.Write &quot;telephoneAssistant = &quot; & telephoneAssistant & &quot;<br />&quot;
Response.Write &quot;department = &quot; & strDepartment & &quot;<br />&quot;
Response.Write &quot;ExtensionAttribute1 = &quot; & strCC & &quot;<br />&quot;
Response.Write &quot;ExtensionAttribute2 = &quot; & strBuilding & &quot;<br />&quot;
Response.Write &quot;ExtensionAttribute3 = &quot; & strDivision & &quot;<br />&quot;
Response.Write &quot;ExtensionAttribute4 = &quot; & strBranch & &quot;<br />&quot;
Response.Write &quot;ExtensionAttribute5 = &quot; & ExtensionAttribute5 & &quot;<br />&quot;
Response.Write &quot;ExtensionAttribute6 = &quot; & ExtensionAttribute6 & &quot;<br />&quot;
Response.Write &quot;ExtensionAttribute7 = &quot; & strGroup & &quot;<br />&quot;
Response.Write &quot;othertelephone = &quot; & othertelephone & &quot;<br />&quot;
Response.Write &quot;samaccountname = &quot; & samaccountname & &quot;<br />&quot;
Response.Write &quot;adspath = &quot; & adspath & &quot;<br />&quot;
Response.Write &quot;FullName = &quot; & strFullName & &quot;<br />&quot;
Response.Write &quot;e-mail = &quot; & strMail & &quot;<br />&quot;
Response.Write &quot;strManager = &quot; & strManager & &quot;<br />&quot;

If Err.Number <> 0 Then
GetInfo = False
Response.Write(Err.Number & &quot;: &quot; & Err.Description) 'always get &quot;The Active Directory datatype cannot be converted to/from a native DS datatype&quot;
Err.Clear
Else
GetInfo = True
End If
%>

<head> <body>

</body>

</html>

Once again, any direction is greatly appreciated.

Thanks!
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top