How to query LDAP using LIKE statement

Last post 09-07-2007 1:52 PM by dpcolgan. 8 replies.

Sort Posts:

  • How to query LDAP using LIKE statement

    09-23-2006, 6:22 PM
    • Loading...
    • Patrik
    • Joined on 04-12-2006, 8:12 AM
    • Stockholm, Sweden
    • Posts 18

    Hi

    How does one query an LDAP with the "adSearcher.Filter " BUT with a LIKE statement ?

    I.e CN like "Mike"

    Regards
    Patrik

     

  • Re: How to query LDAP using LIKE statement

    09-23-2006, 8:11 PM
    Answer
    • Loading...
    • bdesmond
    • Joined on 06-15-2002, 6:02 PM
    • Chicago, IL USA
    • Posts 944
    • ControlGallery
      TrustedFriends-MVPs

    Hi Patrik-

     You would need something like (cn=Mike*) for any cn starting with Mike. cn=*Mike* will get you anything containing Mike.

    --Brian Desmond
    Windows Server MVP - Directory Services
    http://www.briandesmond.com
  • Re: How to query LDAP using LIKE statement

    09-24-2006, 12:10 AM
    • Loading...
    • Patrik
    • Joined on 04-12-2006, 8:12 AM
    • Stockholm, Sweden
    • Posts 18

    Thanks a lot.
    That works like a dream.

    Then another problem arises.
    I have 2 textboxes, one for "givenname" and one for "sn"  (I am building an addressbook, go figure ;-)
    in my query I am using the * in order to perform a LIKE search on whatever is put into the textboxes.
    In order for a post to be found, it needs to contain a value in both attributes.

    I.e. even though I might only put a value in the "givenname" textbox and leave the "sn" blank.
    It will only return values that fits the search criteria AND that has a value in the "sn" field.
    It will NOT display values that fits the search criteria and does not have a value in the "sn" field.

    Any idea ?

    Regards
    Patrik

    ps. in this case I am coding towards ADAM, I have not tested this on AD yet.

  • Re: How to query LDAP using LIKE statement

    09-24-2006, 2:20 AM
    • Loading...
    • Patrik
    • Joined on 04-12-2006, 8:12 AM
    • Stockholm, Sweden
    • Posts 18

    Correction on that:

    It seems that if I perform a query on an attribute that has no value I get an runtime error.
    I am using the code:
    Response.Write(adResult.Properties("telephonenumber")(0).ToString())

    This gives me an error if the value is NULL (Not Set).
    How do I get around that ?

    Regards
    Patrik

  • Re: How to query LDAP using LIKE statement

    09-24-2006, 3:30 AM
    • Loading...
    • Patrik
    • Joined on 04-12-2006, 8:12 AM
    • Stockholm, Sweden
    • Posts 18

    Sorry,
    I found the answer on dunnrys piece on "Common System.DirectoryServices Patterns".

     However, my other problems still manifest itself.

    i.e
    I have 2 textboxes, one for "givenname" and one for "sn"  (I am building an addressbook, go figure ;-)
    in my query I am using the * in order to perform a LIKE search on whatever is put into the textboxes.
    In order for a post to be found, it needs to contain a value in both attributes.

    I.e. even though I might only put a value in the "givenname" textbox and leave the "sn" blank.
    It will only return values that fits the search criteria AND that has a value in the "sn" field.
    It will NOT display values that fits the search criteria and does not have a value in the "sn" field.

    regards
    Patrik

  • Re: How to query LDAP using LIKE statement

    09-24-2006, 12:14 PM
    • Loading...
    • bdesmond
    • Joined on 06-15-2002, 6:02 PM
    • Chicago, IL USA
    • Posts 944
    • ControlGallery
      TrustedFriends-MVPs

    Patrik-

     I would check to see if your firstName textbox is empty before I added that to the search filter.

    --Brian Desmond
    Windows Server MVP - Directory Services
    http://www.briandesmond.com
  • Re: How to query LDAP using LIKE statement

    09-25-2006, 11:44 AM
    • Loading...
    • dunnry
    • Joined on 06-24-2002, 4:17 PM
    • http://directoryprogramming.net
    • Posts 1,806
    • TrustedFriends-MVPs
    bdesmond:

    Patrik-

     I would check to see if your firstName textbox is empty before I added that to the search filter.

    Yep.  I agree.  An easy thing to do is put your text boxes into a panel on the form.  Then at query submit time, just loop through all the controls inside the panel.  If you were clever enough to either name the control or an attribute on the control with the AD attribute name, you can easily perform a dynamic filter building without hardcoding the names of attributes in your code.  This is the way I solved the problem way back in the day and it worked like a charm.

  • Re: How to query LDAP using LIKE statement

    09-26-2006, 12:38 AM
    • Loading...
    • bdesmond
    • Joined on 06-15-2002, 6:02 PM
    • Chicago, IL USA
    • Posts 944
    • ControlGallery
      TrustedFriends-MVPs
    I believe they have a Tag attribute like in Winforms? If so you can just specify the attribute name there. You could also be tricky with viewstate here too.
    --Brian Desmond
    Windows Server MVP - Directory Services
    http://www.briandesmond.com
  • Re: How to query LDAP using LIKE statement

    09-07-2007, 1:52 PM
    • Loading...
    • dpcolgan
    • Joined on 05-26-2005, 8:57 PM
    • Posts 12

    Can anyone point me to an example of using asp.net, connecting to AD, and querying with a "where" clause.? - I'm not new to programming just to AD

Page 1 of 1 (9 items)
Microsoft Communities
Page view counter