locked
How to query LDAP using LIKE statement RRS feed

  • Question

  • User544837646 posted

    Hi

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

    I.e CN like "Mike"

    Regards
    Patrik

     

    Saturday, September 23, 2006 6:22 PM

Answers

  • User1297008538 posted

    Hi Patrik-

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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 23, 2006 8:11 PM

All replies

  • User1297008538 posted

    Hi Patrik-

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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 23, 2006 8:11 PM
  • User544837646 posted

    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.

    Sunday, September 24, 2006 12:10 AM
  • User544837646 posted

    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

    Sunday, September 24, 2006 2:20 AM
  • User544837646 posted

    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

    Sunday, September 24, 2006 3:30 AM
  • User1297008538 posted

    Patrik-

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

    Sunday, September 24, 2006 12:14 PM
  • User1354132231 posted

    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.

    Monday, September 25, 2006 11:44 AM
  • User1297008538 posted
    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.
    Tuesday, September 26, 2006 12:38 AM
  • User1086781591 posted

    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

    Friday, September 7, 2007 1:52 PM
  • User-10017267 posted
    ''' <summary> ''' Method to enumerate OUs ''' </summary> ''' <param name="de">DirectoryEntry to use</param> Public Shared Function EnumerateSpecificOUs(ByVal de As DirectoryEntry, ByVal OUNamePortion As String) As SearchResultCollection Dim deSearch As DirectorySearcher = New DirectorySearcher() deSearch.SearchRoot = de deSearch.Filter = "(&(objectCategory=organizationalUnit)(OU=*" & OUNamePortion & "*))" ' 'deSearch.Filter.Contains(OUNamePortion) Dim src As SearchResultCollection = deSearch.FindAll() Return src End Function
    Sunday, January 17, 2010 10:39 PM