How to read data from Active Directory

It’s probably time to start blogging in English as well. Time to time I wrote a really nice piece of code (don’t comment my style, I’m talking about functionality) and then absolutly forgot about them. So, open my box and publish one of them – how to read data from Active Directory.

I think that it is almost clear, the most interesting line for me is the Cmd.CommandText, where you must specifiy your SQL statement for reading data. All you must find are columns names, where you have your data and then you can easily read them and working.

Dim session As New NotesSession Dim db As NotesDatabase Dim view As NotesView Dim doc As NotesDocument Dim Con As Variant Dim Cmd As Variant Set db = session.CurrentDatabase Set view = db.GetView( "($Users)") Set Con = CreateObject("ADODB.Connection") Set Cmd = CreateObject("ADODB.Command") Con.Provider = "ADsDSOObject" Con.Open "Active Directory Provider" Cmd.ActiveConnection = Con Cmd.CommandText = "SELECT sn,givenName,department,physicalDeliveryOfficeName, telephoneNumber,mobile FROM 'LDAP://OU=YourOUName,DC=int, DC=YourDCName,DC=YourCountry' WHERE objectCategory='person' AND objectClass='user' AND physicalDeliveryOfficeName<>'x' ORDER BY sn" Set RS = Cmd.Execute While Not RS.EOF LastName = RS("sn") FirstName = RS("givenName") Department = RS("department") Office = RS("physicalDeliveryOfficeName") Phone = RS("telephoneNumber") Mobile = RS("mobile") Set doc = view.GetDocumentByKey( LastName + " " + FirstName, True) If Not( doc Is Nothing) Then doc.Department = Department doc.Location = Office doc.OfficePhoneNumber = Phone doc.CellPhoneNumber = Mobile Call doc.Save( True,True) End If RS.MoveNext Wend RS.Close Set RS = Nothing Set Cmd = Nothing Con.Close Set Con = Nothing
This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at

Leave a Reply