Question : Using LDAP to get information from Win Server 2003

I have managed to get the e-mail-adress out of AD (which was very new to me:), but now, of course, I want more!

You have Office, City etc. General information about the user. How do I get it? Can I add new fields to the same query? Can I get all the info and then choose which info I want to use?

I start with the username! We use Windows Server 2003.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
Public Function FindEmail(vUser As String) As String
'
' FindEmail by Hans Kristian 03.08.2009
'
    Dim objConnection As ADODB.Connection, objCommand As ADODB.Command
    Dim strName, objRecordset As ADODB.Recordset, strAD_Context
    
    Const ADS_SCOPE_SUBTREE = 2
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand = CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    Set objCommand.ActiveConnection = objConnection
    
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
        
    objCommand.CommandText = ";(&(objectCategory=User)(samAccountName=" & vUser & "));mail;subtree"
               
    Set objRecordset = objCommand.Execute
    
    If objRecordset.RecordCount = 1 Then
        FindEmail= objRecordset.Fields.Item(0)
    Else
        FindEmail= ""
    End If
        
    objRecordset.Close
    
    objConnection.Close
 
 
End Function

Answer : Using LDAP to get information from Win Server 2003

Hi,

Of course you can obtain several fields with the same LDAP request... As an example here is your request that retrieve mail AND givenname fields :

     ";(&(objectCategory=User)(samAccountName=" & vUser & "));mail,givenname;subtree"

As you can see, you just have to list the fields you want to obtain, separated by a comma.


Then, to get the result easily you can use this syntax :

     objRecordset.Fields("givenname")

instead of your syntax (objRecordset.Fields.Item(0)) that use a field number... Using field number is a source of problem as soon as you change something in your LDAP request. If you change orders of fields in the request then you must change the field numbers in your code.

Using field names, as in the example I gave you, is easier and won't need any change if you change field order in the request.


Have a good day.







Random Solutions  
 
programming4us programming4us