Solved

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

Posted on 2006-10-23
6
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

724 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