Solved

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

Posted on 2006-10-23
6
265 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

733 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