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>
tbaseflugAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sumixCommented:

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)))";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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))";

0
sumixCommented:
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).

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.