Avatar of r3nder
r3nder
Flag for United States of America asked on

Autocomplete in VB.net using LDAP usernames

I am trying to create an auto complete textbox in VB.net using username in LDAP
I am using this in other parts of the code.

Public Shared Function GetUser(ByVal UserName As String) As DirectoryEntry
    'create an instance of the DirectoryEntry
    Dim dirEntry As DirectoryEntry = GetDirectoryObject("/" + GetLDAPDomain())

    'create instance fo the direcory searcher
    Dim dirSearch As New DirectorySearcher(dirEntry)

    dirSearch.SearchRoot = dirEntry
    'set the search filter
    dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
    'deSearch.SearchScope = SearchScope.Subtree;

    'find the first instance
    Dim searchResults As SearchResult = dirSearch.FindOne()

    'if found then return, otherwise return Null
    If Not searchResults Is Nothing Then
        'de= new DirectoryEntry(results.Path,ADAdminUser, _
             ADAdminPassword,AuthenticationTypes.Secure);
        'if so then return the DirectoryEntry object
        Return searchResults.GetDirectoryEntry()
    Else
        Return Nothing
    End If
End Function

Open in new window

Can it be done?
Visual Basic.NETMicrosoft Development.NET ProgrammingMicrosoft Applications

Avatar of undefined
Last Comment
Nasir Razzaq

8/22/2022 - Mon
Nasir Razzaq

You can remove the filter to get all users back and then use that as autocomplete source.
r3nder

ASKER
I removed the filter and changed the function name

In the page load
       
 Try

            ADWrapper.AutoPopulateUser(UserName)
            txtUser.AutoCompleteCustomSource.Clear()
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Open in new window


How whould I add it to a loop to start
Nasir Razzaq

You changed it to do a FindAll instead of FindOne and used a searchresultcollection class right?

http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.findall.aspx
Your help has saved me hundreds of hours of internet surfing.
fblack61
r3nder

ASKER
This is what I did
 Public Shared Function AutoPopulateUser(ByVal UserName As String) As DirectoryEntry
            'create an instance of the DirectoryEntry
            Dim dirEntry As DirectoryEntry = GetDirectoryObject("/" + GetLDAPDomain())

            'create instance fo the direcory searcher
            Dim dirSearch As New DirectorySearcher(dirEntry)

            dirSearch.SearchRoot = dirEntry
            'set the search filter
            'dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
            'deSearch.SearchScope = SearchScope.Subtree;

            'find the first instance
            Dim searchResults As SearchResultCollection = dirSearch.FindAll()

            'if found then return, otherwise return Null
            If Not searchResults Is Nothing Then
                'de= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
                'if so then return the DirectoryEntry object
                Return searchResults.GetEnumerator()
            Else
                Return Nothing
            End If
        End Function

Open in new window

now how do I loop through it to get results?
Thank you for the help
r3nder

ASKER
Do I  use the on text changed for the text box and call the AutoPopulateUser and use the searchresults (Dim serachresults)in the pageload
Nasir Razzaq

Use the ForEach on SearchResults object to populate a list of string.

Call the function to populate the list of string on form load. Then set the textbox properties and it will take care of using the list to make suggestions or append matching names depending on your chosen mode.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
r3nder

ASKER
so.......not getting it?
Dim results As List(Of String) = New List(Of String)
	vals.Add("???")
	vals.Add("???")
	vals.Add("???")

        Try

            ADWrapper.AutoPopulateUser(Nothing)
            Dim results = ADWrapper.AutoPopulateUser(searchResults)
            txtUser.AutoCompleteCustomSource.Clear()
            For Each s In results
                txtUser.AutoCompleteCustomSource.Add(s)

            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Open in new window

Nasir Razzaq

Modify your function so that it returns a list(of string). Then in function

change

            If Not searchResults Is Nothing Then
                'de= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
                'if so then return the DirectoryEntry object
                Return searchResults.GetEnumerator()
            Else
                Return Nothing
            End If

Open in new window



to


    Dim names As New List(of String)
            If Not searchResults Is Nothing Then
               For Each result in SearchResults
                    names.Add(result.GetDirectoryEntry().Name)
               Next
            End If
     Return Names

Open in new window


Once you have this list of names, we will move to next part.
r3nder

ASKER
Done please see code I also had to Dim the result

       
 Public Shared Function AutoPopulateUser(ByVal UserName As String) As List(Of String)
            'Public Shared Function AutoPopulateUser() As DirectoryEntry
            'create an instance of the DirectoryEntry
            Dim dirEntry As DirectoryEntry = GetDirectoryObject("/" + GetLDAPDomain())

            'create instance fo the direcory searcher
            Dim dirSearch As New DirectorySearcher(dirEntry)

            dirSearch.SearchRoot = dirEntry
            'set the search filter
            'dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
            'deSearch.SearchScope = SearchScope.Subtree;

            'find the first instance
            Dim searchResults As SearchResultCollection = dirSearch.FindAll()

            'if found then return, otherwise return Null
            Dim result
            Dim names As New List(Of String)
            If Not searchResults Is Nothing Then
                For Each result In searchResults
                    names.Add(result.GetDirectoryEntry().Name)
                Next
            End If
            Return names

        End Function

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Nasir Razzaq

So now you successfully get a list of names?

Set the list of names as AutoCompleteSource property of the textbox. See example in below link

http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletesource.aspx
r3nder

ASKER
Does this look correct? - I dont know if it takes a  long time to query or if its not working
but its not doing autocomplete

       
Dim mSource As New AutoCompleteStringCollection()
        mSource.Add(ADWrapper.AutoPopulateUser(txtUser.Text).ToString())
        With txtUser
            txtUser.AutoCompleteCustomSource = mSource
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            .Visible = True
        End With

Open in new window

r3nder

ASKER
Here is my updated code can anyone tell me why ADentry in Dim Ds As New List(of ADEntry) undefined?
        Try
            Dim ADEntry As New AutoCompleteStringCollection()
            Dim Ds As New List(Of ADEntry)()
            Ds = New DirectoryEntry("MYSERVER", "MYUSERNAME", "MYPASSWORD")
            Dim dt As New DataTable()
            dt.Columns.Add("Name", GetType(String))
            dt.Columns.Add("Email", GetType(String))
            For Each adentry As ADEntry In Ds
                Dim dr As DataRow = dt.NewRow()
                For i As Integer = 0 To adentry.Information.Count - 1
                    If adentry.Information(i).Key = "mail" Then
                        dr("Email") = adentry.Information(i).Value
                    End If
                    If adentry.Information(i).Key = "displayname" Then
                        dr("Name") = adentry.Information(i).Value
                    End If
                Next
                If Not dr("Email").ToString().Equals("") Then
                    dt.Rows.Add(dr)
                End If
            Next

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
r3nder

ASKER
Could anyone help with this - I have my function in my class it returns a List(Of string)
I am trying to auto populate it in a textbox I have - how do I assign the List(of string) from that function
My Auto complete code is
        Try
            Dim mSource As New AutoCompleteStringCollection()
            mSource.AddRange(ADWrapper.AutoPopulateUser(txtUser.Text))


            With txtUser
                txtUser.AutoCompleteCustomSource = mSource
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .Visible = True
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Open in new window


my function in the class is (and this works)
       Public Shared Function AutoPopulateUser(ByVal UserName As String) As List(Of String)
            'Public Shared Function AutoPopulateUser() As DirectoryEntry
            'create an instance of the DirectoryEntry
            Dim dirEntry As DirectoryEntry = GetDirectoryObject("/" + GetLDAPDomain())

            'create instance fo the direcory searcher
            Dim dirSearch As New DirectorySearcher(dirEntry)

            dirSearch.SearchRoot = dirEntry
            'set the search filter
            'dirSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"
            'deSearch.SearchScope = SearchScope.Subtree;

            'find the first instance
            Dim searchResults As SearchResultCollection = dirSearch.FindAll()

            'if found then return, otherwise return Null
            Dim result
            Dim names As New List(Of String)
            If Not searchResults Is Nothing Then
                For Each result In searchResults
                    names.Add(result.GetDirectoryEntry().Name)
                Next
            End If
            Return names

Open in new window


can someone PLEASE HELP
r3nder

ASKER
Am I close? it keeps telling me UserName is undefined
       
 Try
            Dim mSource As New AutoCompleteStringCollection()
            Dim UserName As New List(Of String)
            Dim suggestions As List(Of String) = New List(Of String)(New String() {ADWrapper.AutoPopulateUser(Username)})


            mSource.AddRange(suggestions.ToArray)


            With txtUser
                txtUser.AutoCompleteCustomSource = mSource
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .Visible = True
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

Open in new window

Any help please?  would be greatly appreciatred
r3nder

ASKER
i found the problem with UserName now it is telling me
Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to 'String'.
Any help is always appreciated
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
r3nder

ASKER
I found it - it works now
ASKER CERTIFIED SOLUTION
Nasir Razzaq

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.