Export (0) Print
Expand All

Sorting the Result Set

Microsoft® Windows® 2000 Scripting Guide

Sorting a large result set can be useful, particularly when you need to group similar values together in an easy-to-read list. Ideally, you should perform searches on indexed attributes because the server performs the sort operation while it is building the result set. Otherwise, the server must generate the entire result set before performing a sort operation. To instruct the server to perform a sort operation, set the Sort On property of the Command object to the attribute you want to sort. If multiple attributes are defined for the sort operation, separate each one with a comma.

Even though the physicalDeliveryOfficeName attribute that appears in Listing 7.32 is not in the global catalog, it is an indexed attribute. Therefore, the server can efficiently sort the result set.

An interesting caveat to searching the directory with referral chasing and sorting enabled is that each domain controller performs the search operation independently. Therefore, the result set is returned in separately sorted blocks. That is, the result set for the root domain will be sorted, and then the result set for each child domain will be sorted. Conversely, a search of the global catalog with sorting enabled returns a single sorted list to the client computer. To obtain a single sorted result set in this way, the attribute must be replicated to the global catalog.

Scripting Steps

Listing 7.32 contains a script that lists all user account types in a domain and the value of an attribute that is not contained in the global catalog. Sorting is enabled on an indexed attribute. To carry out this task, the script performs the following steps:

  1. Create an ADO Connection object to access the Active Directory database by using the ADSI OLE DB provider.

  2. Create an ADO Command object, and assign the ADO connection to it.

  3. Sort the result set by physicalDeliveryOfficeName by setting the Sort On property of the Command object equal to the attributes lDAPDisplayName.

  4. Assign the query string to the CommandText property of the ADO Command object. The string uses LDAP search dialect.

  5. Run the query by assigning the Execute method to the Command object and storing the return value in the RecordSet object, objRecordSet.

  6. Use a While Wend statement to loop through all of the records in the RecordSet object and display the value of the distinguishedName and physicalDeliveryOfficeName attributes.

  7. Move to the next record in the recordset by using the MoveNext method of the RecordSet object. When all records are processed, end the loop.

  8. Close the Connection object.

Listing 7.32 Sorting a Result Set from a Search of the Active Directory

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22


Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

objCommand.Properties("Sort On") = "physicalDeliveryOfficeName"

objCommand.CommandText = _
 "<LDAP://dc=NA,dc=fabrikam,dc=com>;" & _
 "(objectCategory=person);" & _
 "distinguishedName,physicalDeliveryOfficeName;subtree"

Set objRecordSet = objCommand.Execute

While Not objRecordset.EOF
  Wscript.Echo objRecordset.Fields("distinguishedName") & ": " & _
 objRecordset.Fields("physicalDeliveryOfficeName")
 objRecordset.MoveNext
Wend

objConnection.Close

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft