Setting Search Filters
Collapse the table of content
Expand the table of content
This documentation is archived and is not being maintained.

Setting Search Filters 

Search filters enable filtering for specific objects by searching for objects based on attributes associated to the object, such as the objectClass. To do this, set up a filter string with the desired attributes included in the string. For wildcard searches, you can enter any part of the string you wish to search on along with a *, such as (anr=test*). When setting up search filters to use with System.DirectoryServices, follow the syntax rules for LDAP filters. To indicate that a filter is used, use the DirectorySearcher property Filter.

For more information about search filters, see the topic Creating a Query Filter in the MSDN Library at For more information about the objectClass attribute, see the topic objectClass in the MSDN Library at

The following code example shows how to add a search filter.

DirectoryEntry entry = new DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com");
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectClass=user)(anr=test*))";
SearchResultCollection ResEnt = mySearcher.FindAll();
// Handle results.
// Handle exceptions.
© 2016 Microsoft