This is my agent; code is in LS... didn't know SSJS when I build my application
and uses an old fashion way of using a Profile Document to save data and then get data back.
Note you can improve this by calling same agent via Ajax, agent.run or Agent.runWithDocumentContext.
I'm getting LDAP information from configuration documents; this is an example of one of them since I have a document for each country:
Active Directory Info
Domain: NA
Country: United States
IP Address: 172.26.32.23
Port: 389
Where Statement: WHERE objectCategory='CN=Person,CN=Schema,CN=Configuration,DC=ad,DC=mydomain,DC=net' AND sAMAccountName=
Sub Initialize
REM Error Handling
On Error Goto errHandler
REM Global Variables
Dim session As New NotesSession
Dim dbnames As NotesDatabase
Dim viewDomain As NotesView
Dim docPerson As NotesDocument, doc As NotesDocument
Dim docDomain As NotesDocument, docProfile As NotesDocument
Dim ag As NotesAgent
Dim strErrorMsg As String, ip As String, port As String, where As String
Dim username As String, password As String, domain As String
Dim db As NotesDatabase
REM OLE Objects
Dim objConnection As Variant
Dim objCommand As Variant
Dim objRecordSet As Variant
Set db = session.CurrentDatabase
Set ag = session.CurrentAgent
REM Get current document
Set doc = db.GetDocumentByID(ag.ParameterDocID)
If doc Is Nothing Then Error 1000, "Error getting request document"
REM Get Domain document
Set viewDomain = db.Getview("(lookupDomains)")
Set docDomain = viewDomain.Getdocumentbykey(doc.Domain(0), true)
domain = docDomain.Domain(0)
username = doc.UserName(0)
password = doc.Password(0)
ip = docDomain.IPAddress(0)
port = docDomain.Port(0)
where = docDomain.Where(0)
REM Make a lookup to Active Directory
Const ADS_SCOPE_SUBTREE = 2
Const ADS_SECURE_AUTHENTICATION = 1
enhLogAction ||, |Search Active Directory|
enhLogExtendedContent ||, ||, |Username: | + domain + "\" + userName
enhLogExtendedContent ||, ||, ||
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Properties("User ID") = domain + "\" + username
objConnection.Properties("Password") = password
objConnection.Properties("ADSI Flag") = ADS_SECURE_AUTHENTICATION
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 10000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = "SELECT mail FROM 'LDAP://" + ip + ":" + port+ "' " + where + "'" + userName +"'"
enhLogAction ||, "Select Statement"
enhLogExtendedContent ||, ||, objCommand.CommandText
enhLogExtendedContent ||, ||, ||
Set objRecordSet = objCommand.Execute
enhLogAction ||, |Getting Internet address for user | + userName
If ( objRecordSet.RecordCount = 0 ) Then
Error 1000, |No record found for user | + userName
Else
If objRecordSet.RecordCount > 1 Then
Error 1000, objRecordSet.RecordCount + | records found for user | + userName
Exit Sub
Else
enhLogExtendedContent ||, ||, "Internet Address: " + objRecordSet.Fields("mail").value
End If
End If
enhLogExtendedContent ||, ||, ||
REM Getting Person Document
enhLogAction ||, |Getting person document|
Set dbnames = New NotesDatabase( session.CurrentDatabase.Server, "names.nsf")
Dim view As notesview
Dim docs As NotesDocumentCollection
Set view = dbnames.GetView("($Users)")
Set DocPerson = view.GetDocumentByKey (Lcase(objRecordSet.Fields("mail").value),True)
If docPerson Is Nothing Then
Error 1000, "No person document found on Domino Address Book"
Else
enhLogExtendedContent ||, ||, "Notes User: " + docPerson.FullName(0)
End If
Call doc.ReplaceItemValue("NotesUser", docPerson.FullName(0))
REM Assign New Password
Dim pw As String
Dim i As Integer, j As Integer, x As Integer
Dim nchar() As String
For x = 0 To 127 'ASCII-Code
Select Case x
Case 48 To 57
ReDim Preserve nchar(i)
nchar(i) = Chr$(x)
i = i + 1
Case 97 To 122
ReDim Preserve nchar(i)
nchar(i) = Chr$(x)
i = i + 1
End Select
Next
i = i - 1
For j = 1 To 8 'generate the password
pw = pw + nchar(Rnd(x) * i)
Next
Call doc.ReplaceItemValue("NewPassword", pw)
REM End New Password
REM Reset ID Vault
Set docProfile = db.Getprofiledocument("Profile")
Call Session.ResetUserPassword(docProfile.IdVaultServer(0), doc.NotesUser(0), doc.NewPassword(0))
REM Mark request as Completed
Call doc.ReplaceItemValue("Status", "Succesful")
REM Delete Password fields
Call doc.ReplaceItemValue("Password", "xxxxx")
'Call doc.ReplaceItemValue("NewPassword", "xxxxx")
Call doc.Save(True,False)
REM Create Profile document to keep result
Dim replyDoc As NotesDocument
Set replyDoc = db.getProfileDocument("ResetProfile", ag.ParameterDocID)
replyDoc.ReplaceItemValue "Status", "Succesful"
replyDoc.save True, True
REM Disconnect from LDAP
enhLogExtendedContent ||, ||, ||
enhLogAction ||, |Disconnect from LDAP server|
objRecordSet.close
objConnection.close
Exit Sub
errHandler:
strErrorMsg = "Error " & Cstr(Err()) + " - " & Error$ + " - Line: " & Cstr(Erl())
If Not (doc Is Nothing) Then
Call doc.ReplaceItemValue("Status", "Failure")
Call doc.ReplaceItemValue("AgentMsg", strErrorMsg)
REM Delete Password fields
Call doc.ReplaceItemValue("Password", "xxxxx")
Call doc.ReplaceItemValue("NewPassword", "xxxxx")
Call doc.ReplaceItemValue("ConfirmPassword", "xxxxx")
Call doc.Save(True, False)
Set replyDoc = db.getProfileDocument("ResetProfile", ag.ParameterDocID)
replyDoc.ReplaceItemValue "Status", "Failure"
If Err() = 213 Then
replyDoc.ReplaceItemValue "AgentMsg", "Please make sure you entered the correct country, Windows username and password."
Else
replyDoc.ReplaceItemValue "AgentMsg", strErrorMsg
End If
replyDoc.save True, True
End If
enhLogException ||, ||
If objConnection.State = 1 Then objConnection.close
Exit Sub
End Sub