• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 592
  • Last Modified:

AD Query - Multiple Criteria

I am trying to query the department AD attribute but want to query for multiple filters - such as

department=TEST1 or department=TEST2

How would I do this>
  • 2
1 Solution

I suppose you use a DirectorySearcher object. The Filter property for this is build with operator preceding the operands. For example:

System.DirectoryServices.DirectorySearcher ds = new System.DirectoryServices.DirectorySearcher("");
ds.Filter = "(|(department=1)(department=2))"; // means department = 1 OR department = 2
ds.Filter = "(&(objectCategory=computer)(|(department=11)(department=12)(department=13)))";
tbaseflugAuthor Commented:
OK - this works:

deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(department=APDSLN))";

But for soem reason, this does not - when I include the extra department criteria, nothing is returned - when I do one or the other, it works individually, etc. (above):

deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(department=APDSLN)(department=APDSLI))";

You use only one '&' operator, and this condition
   objectClass=user AND objectCategory=person AND department=APDSLN AND department=APDSLI
 cannot be true.

 You need to use '|' (meaning OR) operator also, this way:
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(|(department=APDSLN)(department=APDSLI)))";
   which you can translate:
    (objectClass=user) AND (objectCategory=person) AND (department=APDSLN OR department=APDSLI).


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now