Platform SDK: Directory Services
Searching with ActiveX Data Objects (ADO)
The ActiveX® Data Object model consists of the following objects:
Connection
An open connection to an OLE DB data source such as ADSI.
Command
Defines a specific command to execute against the data source.
Parameters
An optional collection for any parameters to provide to the command object.
Recordset
A set of records from a table, command object, or SQL syntax. A recordset can be created without any underlying Connection object.
Field
A single column of data in a recordset.
Property
A collection of values supplied by the provider for ADO.
Error
Contains details about data access errors, refreshed for each time an error occurs in a single operation.
In order for ADO to communicate with ADSI, you must have at least two ADO objects: Connection and RecordSet. These ADO objects serve to authenticate users and enumerate results, respectively. Typically, you will also use a Command object to maintain an active connection, specify query parameters, such as page size and search scope, and execute a query.
The Connection object loads the OLE DB provider, and validates user credentials. In Visual Basic, you call CreateObject("ADODB.Connection") to create an instance of a Connection object, and then set the Provider property of the Connection object to "ADsDSOObject". "ADODB.Connection" is the ProgID of the Connection object and "ADsDSOObject" is the name of the OLE DB provider in ADSI. If no credentials are specified, the credentials of the currently logged on user are assumed.
The following code snippet illustrates how to create an instance of a Connection object using VBScript.
code:
1
2
| Set con = CreateObject("ADODB.Connection")
con.Provider = "ADsDSOObject" |
The next example is an equivalent Active Server Page (ASP) code snippet.
code:
1
2
3
4
| <%
Set con = Server.CreateObject("ADODB.Connection")
con.Provider = "ADsDSOObject"
%> |
Here is the same example in Visual Basic. Note that you must include the ADO type library (msadoXX.dll) as one of the references in the Visual Basic project.
code:
1
2
| Dim Con As New Connection
con.Provider = "ADsDSOObject" |
You can specify user authentication data by setting the properties of the Connection object. The user-authentication properties supported by ADSI are listed in the following table.
code:
1
2
3
4
5
| Property: Description
"User ID" A string that identifies the user whose security context is used when performing the search. For information about the format of the user name string, see the discussion of the user name parameter in the IADsOpenDSObject::OpenDSObject method. If not specified, the default is the logged on user (or the user being impersonated by the calling process).
"Password" A string that specifies the password of the user identified by "User ID".
"Encrypt Password" A Boolean value that specifies whether the password is encrypted. The default is False.
"ADSI Flag" A set of flags from the ADS_AUTHENTICATION_ENUM enumeration that specify the binding authentication options. The default is zero. |
This example shows how the properties are set before creating the Command object.
code:
1
2
3
4
5
6
| Set oConnect = CreateObject("ADODB.Connection")
oConnect.Provider = "ADsDSOObject"
oConnect.Properties("User ID") = stUser
oConnect.Properties("Password") = stPass
oConnect.Properties("Encrypt Password") = True
oConnect.Open "DS Query", stUser, stPass |
The second ADO object is the Command object. The ProgID is "ADODB.Command". This object lets you issue query statements and other commands to ADSI using the active connection. The Command object uses its ActiveConnection property to maintain an active connection. It also maintains the CommandText property to hold query statements issued by a user. The query statements are expressed in either the SQL dialect or the LDAP dialect. The following code snippets illustrate how to create a Command object.
VBScript example:
code:
1
2
3
4
5
6
7
8
9
10
| Set command = CreateObject("ADODB.Command")
Set command.ActiveConnection = oConnect
command.CommandText =
"SELECT AdsPath, cn FROM 'LDAP://DC=Microsoft,DC=com' WHERE objectClass = '*'"
Visual Basic example: Note you must include ADO type library (msadoXX.dll) as one of the references.
Dim command As New Command
Set command.ActiveConnection = oConnect
command.CommandText =
"<LDAP://DC=Microsoft,DC=com>;(objectClass=*);AdsPath, cn; subTree" |
You can specify search options by setting the property of the Command object actually called Property. Acceptable properties are listed in the following table.
code:
1
2
3
4
5
6
7
8
9
10
11
12
| Property: Description
"Asynchronous" A Boolean value that specifies whether the search is synchronous or asynchronous. The default is False (synchronous). A synchronous search blocks until the server returns the entire result (or for a paged search, the entire page). An asynchronous search blocks until one row of the search results is available, or until the time specified by the "Timeout" property elapses.
"Cache results" A Boolean value that specifies whether the result should be cached on the client side. The default is True; ADSI caches the result set. Turning off this option may be desirable for large result sets.
"Chase referrals" A value from the ADS_CHASE_REFERRALS_ENUM that specifies how the search chases referrals. The default is ADS_CHASE_REFERRALS_EXTERNAL.
"Column Names Only" A Boolean value that indicates that the search should retrieve only the name of attributes to which values have been assigned. The default is False.
"Deref Aliases" A Boolean value that specifies whether aliases of found objects are resolved. The default is False.
"Page size" An integer value that turns on paging and specifies the maximum number of objects to return in a results set. The default is no page size. For more information, see Paging.
"SearchScope" A value from the ADS_SCOPEENUM enumeration that specifies the search scope. The default is ADS_SCOPE_SUBTREE.
"Size Limit" An integer value that specifies the size limit for the search. For Active Directory, the size limit specifies the maximum number of returned objects. The server stops searching once the size limit is reached and returns the results accumulated up to that point. The default is no limit.
"Sort on" A string that specifies a comma-separated list of attributes to use as sort keys. This property works only for directory servers that support the LDAP control for server-side sorting. Active Directory supports the sort control, but it can impact server performance, particularly if the results set is large. Note that Active Directory supports only a single sort key. The default is no sorting.
"Time Limit" An integer value that specifies the time limit (in seconds) for the search. When the time limit is reached, the server stops searching and returns the results accumulated to that point. The default is no time limit.
"Timeout" An integer value that specifies the client-side timeout value (in seconds). This value indicates the time the client waits for results from the server before abandoning the search. The default is no timeout. |
The following code example illustrates how to set search options in Visual Basic.
code:
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
35
36
37
| Dim Com As New Command
Com.Properties("Page Size") = 1000
Com.Properties("Timeout") = 30 'seconds
Com.Properties("searchscope") = ADS_SCOPE_ONELEVEL 'Define in ADS_SCOPEENUM
Com.Properties("Chase referrals") = ADS_CHASE_REFERRALS_EXTERNAL
Com.Properties("Cache Results") = False 'do not cache the result set
The third ADO object is RecordSet. You obtain this object when you invoke the Execute method on a Command object. The primary function of the RecordSet object is to enumerate the result set and obtain the data. The result set can contain values for attributes that have both single or multiple values. Getting a single-valued attribute is straightforward, similar to getting the column value in the relational database (for example, Fields('name').Value). Getting an attribute with multiple values, however, is more challenging. In this case, the Field.Value is an array and you must check the lower and upper bound of the array, as illustrated in the following example.
Set rs = Com.Execute
For i = 0 To rs.Fields.Count - 1
Debug.Print rs.Fields(i).Name, rs.Fields(i).Type
Next i
'--------------------------
'Navigate the record set.
'--------------------------
rs.MoveFirst
lstResult.Clear 'Clear the user interface.
While Not rs.EOF
For i = 0 To rs.Fields.Count - 1
'For Multi Value attribute
If rs.Fields(i).Type = adVariant And Not (IsNull(rs.Fields(i).Value)) Then
Debug.Print rs.Fields(i).Name, " = "
For j = LBound(rs.Fields(i).Value) To UBound(rs.Fields(i).Value)
Debug.Print rs.Fields(i).Value(j), " # "
lstResult.AddItem rs.Fields(i).Value(j)
Next j
Else
'For Single Value attribute.
Debug.Print rs.Fields(i).Name, " = ", rs.Fields(i).Value
lstResult.AddItem rs.Fields(i).Value
End If
Next i
rs.MoveNext
Wend |
The following example disables the user accounts on an LDAP server using Visual Basic.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| Dim X as IADs
Dim con As New Connection, rs As New Recordset
Dim MyUser As IADsUser
con.Provider = "ADsDSOObject"
con.Open "Active Directory Provider", "CN=Foobar,CN=Users,DC=MICROSOFT,DC=COM,O=INTERNET", "Password"
Set rs = con.Execute("<LDAP://MyMachine/DC=MyDomain,DC=Microsoft,DC=com>;(objectClass=User);ADsPath;onelevel")
While Not rs.EOF
' Bind to the object to make changes
' to it since ADO is currently read-only.
MyUser = GetObject(rs.Fields(0).Value)
MyUser.AccountDisabled = True
rs.MoveNext
Wend |
For more information about the ADO object model, see ActiveX Data Objects.
Microsoft Platform SDK, February 2001 Edition.
This content last built on Thursday, February 01, 2001.