Directory Searcher Filter

Posted on 2012-08-20
Last Modified: 2012-08-20
I have a class that's getting the information I want but running too long

I THINK I need a filter on my Searcher.FindAll()

I want to look for a particular "user" based on thier login name

The FUnction is called with the following function and the "incomingUsername" is set with

 Public Function getUserInfo(ByVal incomingUserName As String) As String
        getUserData = LoadADSIDetails("", incomingUserName)
        Return getUserData
    End Function

Following code starts a loop...
I want to apply a filter somehow

 Public Function LoadADSIDetails(ByVal Query As String, ByVal incomingUserName As String) As String

        Dim counter As Int16 = 0
        Dim searcher As New DirectorySearcher("")
        Dim DataToWrite As String = Nothing
            If Query.Trim.Length = 0 Then
                searcher.Filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(objectCategory=user))"
                searcher.Filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(objectCategory=user)(" + Query + "))"
            End If
            searcher.SearchScope = SearchScope.Subtree

            Dim FirstName, SurName, Email, UserName, GroupName As String

            For Each result As SearchResult In searcher.FindAll()


Open in new window

Question by:lrbrister
    LVL 12

    Accepted Solution

    Here's an example with a filter:

    Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://my.domain.local";)
     Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot)
     Dim oresult As SearchResultCollection
     Dim result As SearchResult
    osearcher.Filter = "(&(sAMAccountName=jsmith))"
     oresult = osearcher.FindAll
    For Each result In oresult
      If Not result.GetDirectoryEntry.Properties("SAMAccountName").Value Is
     Nothing Then
     & "<br />")
      End If
    LVL 35

    Expert Comment

    what is Query?

    Author Closing Comment



    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
    This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

    734 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

    25 Experts available now in Live!

    Get 1:1 Help Now