Solved

LDAP, ASP.NET, and VB. Can't populate dropdownlist with email addresses from exchange server.

Posted on 2006-10-23
6
262 Views
Last Modified: 2012-06-21
I have tried several examples but can't seem to get the right sequence of pieces of code to make it happen. Any suggestions on how to do this.
0
Comment
Question by:wrwiii12
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:bigphuckinglizard
ID: 17789830
Converted from c# so may be a bit funky, but try this... you also need to make sure you have a reference to System.DirectoryServices in your project.

               Dim LDAPConnection As New DirectoryEntry("LDAP://domain/OU=yourou,DC=domain,DC=tld", ConfigurationManager.AppSettings("LDAPUsername"), ConfigurationManager.AppSettings("LDAPPassword"))
                Try
                    Dim Searcher As New DirectorySearcher(LDAPConnection)
                    Searcher.Filter = "(objectclass=user)"
                    Searcher.PropertiesToLoad.Add("mail")
                    Dim results  as SearchResultCollection = Searcher.FindAll()
                    For Each (result as SearchResult in results) {
                        Dim email as String= LDAPConnection.Properties("mail")[0].ToString()
                        dropdownlist1.items.add(new ListItem(email))
                    Next
                Finally
                    LDAPConnection.Close() //very bad things happen if connections to ad are left open!!!
                End Try
0
 
LVL 4

Author Comment

by:wrwiii12
ID: 17791237
Almost there! (I think)
Name 'result' is not declared.

I know what the domain is, but what are the other parameters OU, DC, and DC.
If my domain is called "ecc.dept.local", how should the string look?
0
 
LVL 4

Author Comment

by:wrwiii12
ID: 17791362
Here is the revised code that at least compiles. It runs but throws and exception
at the LDAPConnection.Properties line:
Index was out of range. Must be non-negative and less than the size of the collection.


Dim LDAPConnection As New DirectoryEntry("LDAP://eccdept.local",
ConfigurationManager.AppSettings("LDAPUsername"),
ConfigurationManager.AppSettings("LDAPPassword"))
        Try
            Dim Searcher As New DirectorySearcher(LDAPConnection)
            Searcher.Filter = "(objectclass=user)"
            Searcher.PropertiesToLoad.Add("mail")
            Dim result As SearchResult
            Dim results As SearchResultCollection = Searcher.FindAll()
            For Each result In results
                Dim email As String  <--(seperated it for clarity on my part) -->
                email = LDAPConnection.Properties("mail")(0).ToString()  <-- line it errors on -->
                DropDownList1.Items.Add(New ListItem(email))
            Next
        Finally
            LDAPConnection.Close() '//very bad things happen if connections to ad are left open!!!
        End Try
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Expert Comment

by:bigphuckinglizard
ID: 17793884
you need to know the netbios name of your domain as well, it should be

Dim LDAPConnection As New DirectoryEntry("LDAP://NETBIOSNAME/dc=eccdept,dc=local", username, password)

and i think i've found that vb needs an ienumerator in the loop:

            For Each result In results
                Dim email As String  <--(seperated it for clarity on my part) -->
                Dim i As IEnumerator = LDAPConnection.Properties("mail").GetEnumerator()                  
                While i.MoveNext
                        DropDownList1.Items.Add(i.Current.ToString())
                  End While
            Next
0
 
LVL 4

Author Comment

by:wrwiii12
ID: 17795680
it executes the results.count which the mouse tooltip says that there are 328 in count
it passes on through the Dim i as IENumerator and starts to cycle through the while i.movenext
it never drops inside the while loop
0
 
LVL 6

Accepted Solution

by:
bigphuckinglizard earned 200 total points
ID: 17796045
i've loaded up vb and got it sussed

Dim LDAPConnection As New DirectoryEntry("LDAP://NETBIOSNAME/dc=eccdept,dc=local", username, password)

        Try
            Dim Searcher As New DirectorySearcher(LDAPConnection)
            Searcher.Filter = "(objectclass=user)"
            Searcher.PropertiesToLoad.Add("mail")
            Dim result As SearchResult
            Dim results As SearchResultCollection = Searcher.FindAll()
            For Each result In results
                If result.Properties.Contains("mail") Then
                    DropDownList1.Items.Add(result.Properties("mail")(0).ToString())
                End If
            Next
        Finally
            LDAPConnection.Close() '//very bad things happen if connections to ad are left open!!!
        End Try
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do i learn Story Board for IOS ? 3 99
Retrieve response header - authorization bearer token 34 115
tomcat startup error 5 59
Fulfillment API php code sample 1 38
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

776 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