AD Query - Multiple Criteria

Posted on 2006-04-14
Last Modified: 2008-02-01
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>
Question by:tbaseflug
    LVL 12

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

    Author Comment

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

    LVL 12

    Expert Comment

    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).


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now