Mohammed Thabet
Mohammed Thabet

Reputation: 21677

LDAP with objectCategory query problem

I want to create LDAP query to filter printers by name and location and model

deSearch.Filter = String.Format("(&(&(&(&(objectCategory=printQueue)(printername={0}))(location={1}))(driverName={2})))", queueName, location, modelNumber);

I create this but it didn't run correctly

  1. The first problem is to search by all search criteria together
  2. if one of criteria is empty or null ,I set it by * to get all results .Is it correct ?

All ideas are welcomed

Upvotes: 1

Views: 1446

Answers (2)

Mohammed Thabet
Mohammed Thabet

Reputation: 21677

Accoroding to EJP reply I created the code for that here

 StringBuilder filter=new StringBuilder("(&(objectClass=printQueue)");
        if (!string.IsNullOrEmpty(queueName))
            filter.Append("(printerName=*"+queueName+"*)") ;

        if (!string.IsNullOrEmpty(location))

            filter.Append("(location=*" + location + "*)");

        if (!string.IsNullOrEmpty(modelNumber))

            filter.Append("(driverName=*" + modelNumber + "*)");

        filter.Append(")");

        deSearch.Filter = filter.ToString();

Upvotes: 0

user207421
user207421

Reputation: 310980

You only need one & operator. They are n-ary, not binary, operators in LDAP filter expressions:

(&(objectCategory=printQueue)(printername={0})(location={1})(driverName={2}))

(RFC 2254 defines what follows the & (or |) as a SET OF Filter, not as exactly two filters. This is about the only good reason I can see why they chose that ghastly prefix notation.)

I would personally supply 'printQueue' as an argument as well in a query like this.

'*' will match any attribute value, but it requires the attribute to be actually present, i.e. for the objectClass to have such an attribute.

Upvotes: 3

Related Questions