How to populate a dropdown with active directory OU in an asp.net c# web forms application?

Hello Experts.

I have read several posts on this topic, but I still need help.  I have an asp.net c# web forms application.  I would like to populate a dropdown list with an organizational unit in active directory.  I have the server name (xyz.org), search base (DC=xyz,DC=org), Port Number (389), user name, password and organizational unit (Standard Users).

I have added the following to the connectionstring section of the web.config file:
<add name="ADCnxn" connectionString="LDAP:\\xyz.org\OU=Standard User,DC=xyz,DC=org"/>

Open in new window

I have also added the System.DirectoryServices reference to the project.  I just need to know the next steps.  Any help would be greatly appreciated.

I am using Visual Studio 2013 and the framework version is 4.5.1.

Please let me know if additional information is required.

Thanks,
J
ferguson_jeraldAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
I do not know if this is the cause of your issue, but you are using the wrong slash. You need to use the forward leaning slash ( / ).
David L. HansenCEOCommented:
Does the connection string work? And if so, are you just looking to populate the dropdown box at this point?
it_saigeDeveloperCommented:
Here is a simple page to display items:

Default.aspx -
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="EE_Q28712608._Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    	<asp:Label ID="Label1" runat="server" Text="LDAP Root:"></asp:Label>&nbsp;&nbsp;<asp:TextBox ID="TextBox1" runat="server" Width="450px">LDAP://DC=corp,DC=com</asp:TextBox>
    	<br /><br />
	<asp:Label ID="Label2" runat="server" Text="Choose display object type:"></asp:Label>&nbsp;&nbsp;<asp:DropDownList OnSelectedIndexChanged="OnSelectedIndexChanged" ID="DropDownList1" runat="server" AutoPostBack="True">
		    <asp:ListItem Selected="True" Value="-1">--- Select Display Object ---</asp:ListItem>
		    <asp:ListItem Value="0">Organizational Units</asp:ListItem>
		    <asp:ListItem Value="1">Groups</asp:ListItem>
		    <asp:ListItem Value="2">Users</asp:ListItem>
	</asp:DropDownList>
    	<br /><br />
	<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>
</html>

Open in new window

Default.aspx.designer.vb -
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated. 
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On


Partial Public Class _Default

	'''<summary>
	'''form1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm

	'''<summary>
	'''Label1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents Label1 As Global.System.Web.UI.WebControls.Label

	'''<summary>
	'''TextBox1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents TextBox1 As Global.System.Web.UI.WebControls.TextBox

	'''<summary>
	'''Label2 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents Label2 As Global.System.Web.UI.WebControls.Label

	'''<summary>
	'''DropDownList1 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents DropDownList1 As Global.System.Web.UI.WebControls.DropDownList

	'''<summary>
	'''DropDownList2 control.
	'''</summary>
	'''<remarks>
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	'''</remarks>
	Protected WithEvents DropDownList2 As Global.System.Web.UI.WebControls.DropDownList
End Class

Open in new window

Default.aspx.vb -
Imports System.ComponentModel
Imports System.DirectoryServices

Public Class _Default
	Inherits System.Web.UI.Page

	ReadOnly ADObjects As BindingList(Of DirectoryEntry) = New BindingList(Of DirectoryEntry)

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		DropDownList2.DataSource = ADObjects
	End Sub

	Private Sub LoadListBox(ByVal selected As Integer)
		If ADObjects.Count > 0 Then
			ADObjects.Clear()
		End If
		Try
			Dim root As New DirectoryEntry(TextBox1.Text)
			Dim searcher As New DirectorySearcher()
			searcher.SearchRoot = root
			Select Case selected
				Case -1
					Return
				Case 0
					searcher.Filter = "(objectCategory=organizationalUnit)"
					Exit Select
				Case 1
					searcher.Filter = "(objectCategory=group)"
					Exit Select
				Case 2
					searcher.Filter = "(&(objectCategory=person)(objectClass=user))"
					Exit Select
			End Select
			For Each result As SearchResult In searcher.FindAll()
				Dim de As DirectoryEntry = result.GetDirectoryEntry()
				If de IsNot Nothing Then ADObjects.Add(de)
			Next
			DropDownList2.DataTextField = "Name"
			DropDownList2.DataBind()
		Catch ex As Exception

		End Try
	End Sub

	Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
		If TypeOf sender Is DropDownList Then
			Dim ddl = DirectCast(sender, DropDownList)
			Dim selected As Integer = -1
			If Integer.TryParse(ddl.SelectedValue, selected) Then
				LoadListBox(selected)
			End If
		End If
	End Sub
End Class

Open in new window

Produces the following output -Initial LoadEntering my AD root and selecting Organizational Units-saige-
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

David L. HansenCEOCommented:
It looks like you are populating the dropdown list just fine. I'm not sure what the problem is. Can you elaborate a little? Do you want folder names to be in the dropdown list?
ferguson_jeraldAuthor Commented:
David - yes, the connection string works, I just need to populate the dropdown list.  I'm not sure where to start with that.  I would like to populate the dropdown with the usernames from the "Standard Users" folder.  I just don't know how to code it.  The id for the dropdown list is "usersddl".
David L. HansenCEOCommented:
Here is a link to show how someone is doing something very similar where they incorporate PowerShell.
http://www.experts-exchange.com/questions/28697908/Active-directory-user-shared-folder-cleanup.html#a40886542

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.