Platform SDK: Active Directory, ADSI, and Directory Services

Search Filter Syntax

Search filters allow you to define search criteria and give you better control to achieve more effective and efficient searches. For example, you may be interested in all the users whose surname is "Smith". Or you may want to find out all the team members reporting to the manager named "Mary Jane". Search filters help you to manage complex queries more easily.

ADSI supports the LDAP search filters as defined in RFC2254. These search filters are represented by Unicode strings. Some examples of LDAP search filters are as follows:

(objectClass=*)
All objects
(&(objectClategory=person)(objectClass=user)(!cn=andy))
All user objects but "andy"
(sn=sm*)
All objects with a surname that starts with "sm"
(&(objectCategory=person)(objectClass=contact)(|(sn=Smith)(sn=Johnson)))
All contacts with a surname equal to "Smith" or "Johnson"

These search filters use one of the following formats:

<filter>=(<attribute><operator><value>)

or

(<operator><filter1><filter2>)

Some operators that are frequently used for search filters are listed in the following table:

= Equal to
~= Approximately equal to
<= Lexicographically less than or equal to
>= Lexicographically greater than or equal to
& AND
| OR
! NOT

The ADSI search filters are used in two ways. They form a part of the LDAP dialect for submitting queries through the OLE DB provider. They are also used with the IDirectorySearch interface.