locked
Need guidance with DirectorySearcher Filter syntax RRS feed

  • Question

  • User-1979106533 posted
    Greetings, I am *brand new* to retrieving information from the Active Directory from a C# class. I understand in principle what I need to be doing (I need a DirectorySearcher and I need to set Filter syntax) but I am hung up on the Filter syntax. My requirements are straightforward: I need to retrieve all users who are listed under a nested "Business Units" folder in the Active Directory. The folder structure in the Active Directory looks like this: root/mycompany.net/Business Units I need to return all users listed in the Business Units folder which is nested several levels from the directory root. Can someone please suggest what the syntax should be, in general or in the specific. Thanks for your help, BCB
    Thursday, December 21, 2006 10:40 AM

Answers

  • User1913348343 posted

    Hi,

        I am new to DirectorySearcher. Here is my question.

    I want to search firstname + lastname = somename. But I can only search by firstname or lastname like this

    searcher.filter = "(&(firstname=" & txtname.text & ")"

    searcher.filter = "(&(lastname=" & txtname.text & ")"

    I want to do soething like this:

    searcher.filter = "(&(firstname + lastname=" & txtname.txt & ")"

    But this doesn't work.

    Thanks

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2007 4:33 AM

All replies

  • User928782211 posted

    "(&(objectCategory=user)(cn=*" & Sercher & "*))"

    Sercher is only a varible that retrives a value from a textbox, you can remove that.

    Friday, December 22, 2006 8:15 AM
  • User228121329 posted

    Hi ,

    i have used the following code to get the list of containers in the Active Directory: 

    DirectorySearcher srch2 = new DirectorySearcher("(objectCategory=container)"); 

    when i used "distinguishedname" i get the following list:

        CN=Users,DC=Domain,DC=local
        CN=Computers,DC=Domain,DC=local
        CN=System,DC=Domain,DC=local
        CN=ForeignSecurityPrincipals,DC=Domain,DC=local
        CN=Program Data,DC=Domain,DC=local
        CN=Microsoft,CN=Program Data,DC=Domain,DC=local
        CN=WinsockServices,CN=System,DC=Domain,DC=local
        CN=RpcServices,CN=System,DC=Domain,DC=local
        CN=Meetings,CN=System,DC=Domain,DC=local
        CN=Policies,CN=System,DC=Domain,DC=local
        .............

       ..............

     

    This list is too big...

    But actually i want the list where only one CN value exists.
     

     ie     CN=Users,DC=Domain,DC=local
        CN=Computers,DC=Domain,DC=local
        CN=System,DC=Domain,DC=local
        CN=ForeignSecurityPrincipals,DC=Domain,DC=local
        CN=Program Data,DC=Domain,DC=local

     Could u tell me how could i get it?

    Also i can use filter, but it may be possible that on any other domain  the list may be different . pls guide me.

     

    Thanks, waiting for the reply.

    Gaurav Garg 

     

    Saturday, December 30, 2006 4:56 AM
  • User1913348343 posted

    Hi,

        I am new to DirectorySearcher. Here is my question.

    I want to search firstname + lastname = somename. But I can only search by firstname or lastname like this

    searcher.filter = "(&(firstname=" & txtname.text & ")"

    searcher.filter = "(&(lastname=" & txtname.text & ")"

    I want to do soething like this:

    searcher.filter = "(&(firstname + lastname=" & txtname.txt & ")"

    But this doesn't work.

    Thanks

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2007 4:33 AM
  • User1588321482 posted

    You can't do comparisons like this because the filter in the searcher is passing a LDAP query. From what you are describng you would need to use a filter such as (&(objectcategory=person)(objectclass=user)) and then in the PropertiestoLoad part of the searcher ask for "givenName" and "sn". When you then do a findall() the resulting collection can be iterated to find out if givenName = Sn. Also be aware that LDAP in AD will only return 1,000 objects at a time so you may need to use paging to get more.

    The following links might help understand the LDAP query:

    http://msdn.microsoft.com/en-us/library/ms675768.aspx

     

    Friday, June 29, 2007 4:08 AM
  • User-1303272314 posted

    Hi,

    I think that you will have to separate the firtname and lastname, like this:

    searcher.filter = "(|(firstname=" & txtFirstName.text & ")(lastname=" & txtLastName.text & ")"
    Note:

    &     =      AND
    |      =      OR

    Thursday, October 1, 2009 10:01 AM