Display Active Directory Users in a ListBox

J

j.m.osterman

I haven't found exactly what I've been trying to do.

All I am trying to do for now is just display usernames from Active
Directory into a ListBox control on a page. I have found some code
however that will just capture a Full Name from Text Box and display
information from the Active Directory into a DataTable. I will display

that code below.


How can I change this code to make it display on the Page Load all
users into a ListBox control?


Here is what I have right now that I was to change to make it work in a

listbox and not rely on the entry from the textbox...


<!--Beginning of Page -->
<%@ Page Language="C#" %>


<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
body {
font-family: Arial, Verdana;
font-size: 11pt;
font-color: #000000;
margin: 0;
padding: 0 10px 0 10px;
text-align: left
}
</style>


</head>
<body>
<form id="Form1" runat="server">
<asp:ScriptManager ID="AtlasScriptCore" runat="server"
EnablePartialRendering="true" />
<div id="content">
<h3>
Active Directory Searcher</h3>
<div id="searchbox">
Type Users Surname<br />
<asp:TextBox ID="txtFullName" runat="server">
</asp:TextBox>
<asp:Button ID="btnDetails" runat="server" Text="Get AD

Details" OnClick="GetADDetails" />


</div><br />
<div id="Results">
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="ADUserProperties"
runat="server">
</asp:GridView>
<asp:Literal runat="server" ID="SysMessage"
EnableViewState="false" ></asp:Literal>
</ContentTemplate>
<Triggers>
<asp:postBackTrigger ControlID="btnDetails" />
</Triggers>
</asp:UpdatePanel>


</div>
</div>
</form>
</body>
</html>


<script runat="server">
protected void GetADDetails(Object sender , EventArgs e )
{
// Take the value from the input box and pull back a few AD
details
DataTable UserProperties = null;
UserProperties = GetUserByDisplayName(txtFullName.Text);
// Display data if we have any or show warning
if (UserProperties != null)
{
ADUserProperties.DataSource = UserProperties;
ADUserProperties.DataBind();
}
else
{
// Show no records
SysMessage.Text = "<p style=\"color: red;\">Could not find
any details for this user. <br />" +
"Please check that the users name is
correct.</p>";
}
}


protected DataTable GetUserByDisplayName(String fullUserName)
{
DirectoryEntry de = new
DirectoryEntry(ConfigurationManager.AppSettings.Get("ADPath"));
// Authentication details
de.Username =
ConfigurationManager.AppSettings.Get("ADServiceAccount"); //DOMAIN\User

de.Password =
ConfigurationManager.AppSettings.Get("ADServiceAccountPassword");
de.AuthenticationType = AuthenticationTypes.FastBind;


DirectorySearcher DirectorySearcher = new
DirectorySearcher(de);
DirectorySearcher.ClientTimeout = TimeSpan.FromSeconds(30);
// load the properties we are interested in
DirectorySearcher.PropertiesToLoad.Add("cn");
DirectorySearcher.PropertiesToLoad.Add("sAMAccountName");
DirectorySearcher.PropertiesToLoad.Add("mail");
DirectorySearcher.PropertiesToLoad.Add("displayName");
DirectorySearcher.PropertiesToLoad.Add("mDBStorageQuota");
DirectorySearcher.PropertiesToLoad.Add("title");


DirectorySearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName");
DirectorySearcher.PropertiesToLoad.Add("telephoneNumber");
// filter it on exact entry - NOTE no wild card
DirectorySearcher.Filter = "(displayName=" +
fullUserName.Trim() + ")";


SearchResult result;
// There should only be one entry
result = DirectorySearcher.FindOne();
if (result != null)
{
// Create a table an populate it with properties to bind to

gridview
DataTable myTable = new DataTable("ActiveDir");
myTable.Columns.Add(new DataColumn("Key",
System.Type.GetType("System.String")));
myTable.Columns.Add(new DataColumn("Value",
System.Type.GetType("System.String")));
DataRow myRow;
foreach (string propname in
result.Properties.PropertyNames)
{
foreach (Object objValue in
result.Properties[propname])
{
myRow = myTable.NewRow();
myRow[0] = propname;
myRow[1] = objValue.ToString();
myTable.Rows.Add(myRow);
}
}
return myTable;
}
else
{
return null;
}
}


<!--End of Page -->


Any help would greatly appreciated!


Thanks,


Jonathan
 
C

Chan Ming Man

Check the Datasource and Datamember properties. I do not see any binding
code from below maybe I miss it.

chanmm


I haven't found exactly what I've been trying to do.

All I am trying to do for now is just display usernames from Active
Directory into a ListBox control on a page. I have found some code
however that will just capture a Full Name from Text Box and display
information from the Active Directory into a DataTable. I will display

that code below.


How can I change this code to make it display on the Page Load all
users into a ListBox control?


Here is what I have right now that I was to change to make it work in a

listbox and not rely on the entry from the textbox...


<!--Beginning of Page -->
<%@ Page Language="C#" %>


<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
body {
font-family: Arial, Verdana;
font-size: 11pt;
font-color: #000000;
margin: 0;
padding: 0 10px 0 10px;
text-align: left
}
</style>


</head>
<body>
<form id="Form1" runat="server">
<asp:ScriptManager ID="AtlasScriptCore" runat="server"
EnablePartialRendering="true" />
<div id="content">
<h3>
Active Directory Searcher</h3>
<div id="searchbox">
Type Users Surname<br />
<asp:TextBox ID="txtFullName" runat="server">
</asp:TextBox>
<asp:Button ID="btnDetails" runat="server" Text="Get AD

Details" OnClick="GetADDetails" />


</div><br />
<div id="Results">
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="ADUserProperties"
runat="server">
</asp:GridView>
<asp:Literal runat="server" ID="SysMessage"
EnableViewState="false" ></asp:Literal>
</ContentTemplate>
<Triggers>
<asp:postBackTrigger ControlID="btnDetails" />
</Triggers>
</asp:UpdatePanel>


</div>
</div>
</form>
</body>
</html>


<script runat="server">
protected void GetADDetails(Object sender , EventArgs e )
{
// Take the value from the input box and pull back a few AD
details
DataTable UserProperties = null;
UserProperties = GetUserByDisplayName(txtFullName.Text);
// Display data if we have any or show warning
if (UserProperties != null)
{
ADUserProperties.DataSource = UserProperties;
ADUserProperties.DataBind();
}
else
{
// Show no records
SysMessage.Text = "<p style=\"color: red;\">Could not find
any details for this user. <br />" +
"Please check that the users name is
correct.</p>";
}
}


protected DataTable GetUserByDisplayName(String fullUserName)
{
DirectoryEntry de = new
DirectoryEntry(ConfigurationManager.AppSettings.Get("ADPath"));
// Authentication details
de.Username =
ConfigurationManager.AppSettings.Get("ADServiceAccount"); //DOMAIN\User

de.Password =
ConfigurationManager.AppSettings.Get("ADServiceAccountPassword");
de.AuthenticationType = AuthenticationTypes.FastBind;


DirectorySearcher DirectorySearcher = new
DirectorySearcher(de);
DirectorySearcher.ClientTimeout = TimeSpan.FromSeconds(30);
// load the properties we are interested in
DirectorySearcher.PropertiesToLoad.Add("cn");
DirectorySearcher.PropertiesToLoad.Add("sAMAccountName");
DirectorySearcher.PropertiesToLoad.Add("mail");
DirectorySearcher.PropertiesToLoad.Add("displayName");
DirectorySearcher.PropertiesToLoad.Add("mDBStorageQuota");
DirectorySearcher.PropertiesToLoad.Add("title");


DirectorySearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName");
DirectorySearcher.PropertiesToLoad.Add("telephoneNumber");
// filter it on exact entry - NOTE no wild card
DirectorySearcher.Filter = "(displayName=" +
fullUserName.Trim() + ")";


SearchResult result;
// There should only be one entry
result = DirectorySearcher.FindOne();
if (result != null)
{
// Create a table an populate it with properties to bind to

gridview
DataTable myTable = new DataTable("ActiveDir");
myTable.Columns.Add(new DataColumn("Key",
System.Type.GetType("System.String")));
myTable.Columns.Add(new DataColumn("Value",
System.Type.GetType("System.String")));
DataRow myRow;
foreach (string propname in
result.Properties.PropertyNames)
{
foreach (Object objValue in
result.Properties[propname])
{
myRow = myTable.NewRow();
myRow[0] = propname;
myRow[1] = objValue.ToString();
myTable.Rows.Add(myRow);
}
}
return myTable;
}
else
{
return null;
}
}


<!--End of Page -->


Any help would greatly appreciated!


Thanks,


Jonathan
 
J

JMO

No this code works just want to show all the AD users in a listbox
instead of the datatable shown above.

Jonathan
Check the Datasource and Datamember properties. I do not see any binding
code from below maybe I miss it.

chanmm


I haven't found exactly what I've been trying to do.

All I am trying to do for now is just display usernames from Active
Directory into a ListBox control on a page. I have found some code
however that will just capture a Full Name from Text Box and display
information from the Active Directory into a DataTable. I will display

that code below.


How can I change this code to make it display on the Page Load all
users into a ListBox control?


Here is what I have right now that I was to change to make it work in a

listbox and not rely on the entry from the textbox...


<!--Beginning of Page -->
<%@ Page Language="C#" %>


<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
body {
font-family: Arial, Verdana;
font-size: 11pt;
font-color: #000000;
margin: 0;
padding: 0 10px 0 10px;
text-align: left
}
</style>


</head>
<body>
<form id="Form1" runat="server">
<asp:ScriptManager ID="AtlasScriptCore" runat="server"
EnablePartialRendering="true" />
<div id="content">
<h3>
Active Directory Searcher</h3>
<div id="searchbox">
Type Users Surname<br />
<asp:TextBox ID="txtFullName" runat="server">
</asp:TextBox>
<asp:Button ID="btnDetails" runat="server" Text="Get AD

Details" OnClick="GetADDetails" />


</div><br />
<div id="Results">
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="ADUserProperties"
runat="server">
</asp:GridView>
<asp:Literal runat="server" ID="SysMessage"
EnableViewState="false" ></asp:Literal>
</ContentTemplate>
<Triggers>
<asp:postBackTrigger ControlID="btnDetails" />
</Triggers>
</asp:UpdatePanel>


</div>
</div>
</form>
</body>
</html>


<script runat="server">
protected void GetADDetails(Object sender , EventArgs e )
{
// Take the value from the input box and pull back a few AD
details
DataTable UserProperties = null;
UserProperties = GetUserByDisplayName(txtFullName.Text);
// Display data if we have any or show warning
if (UserProperties != null)
{
ADUserProperties.DataSource = UserProperties;
ADUserProperties.DataBind();
}
else
{
// Show no records
SysMessage.Text = "<p style=\"color: red;\">Could not find
any details for this user. <br />" +
"Please check that the users name is
correct.</p>";
}
}


protected DataTable GetUserByDisplayName(String fullUserName)
{
DirectoryEntry de = new
DirectoryEntry(ConfigurationManager.AppSettings.Get("ADPath"));
// Authentication details
de.Username =
ConfigurationManager.AppSettings.Get("ADServiceAccount"); //DOMAIN\User

de.Password =
ConfigurationManager.AppSettings.Get("ADServiceAccountPassword");
de.AuthenticationType = AuthenticationTypes.FastBind;


DirectorySearcher DirectorySearcher = new
DirectorySearcher(de);
DirectorySearcher.ClientTimeout = TimeSpan.FromSeconds(30);
// load the properties we are interested in
DirectorySearcher.PropertiesToLoad.Add("cn");
DirectorySearcher.PropertiesToLoad.Add("sAMAccountName");
DirectorySearcher.PropertiesToLoad.Add("mail");
DirectorySearcher.PropertiesToLoad.Add("displayName");
DirectorySearcher.PropertiesToLoad.Add("mDBStorageQuota");
DirectorySearcher.PropertiesToLoad.Add("title");


DirectorySearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName");
DirectorySearcher.PropertiesToLoad.Add("telephoneNumber");
// filter it on exact entry - NOTE no wild card
DirectorySearcher.Filter = "(displayName=" +
fullUserName.Trim() + ")";


SearchResult result;
// There should only be one entry
result = DirectorySearcher.FindOne();
if (result != null)
{
// Create a table an populate it with properties to bind to

gridview
DataTable myTable = new DataTable("ActiveDir");
myTable.Columns.Add(new DataColumn("Key",
System.Type.GetType("System.String")));
myTable.Columns.Add(new DataColumn("Value",
System.Type.GetType("System.String")));
DataRow myRow;
foreach (string propname in
result.Properties.PropertyNames)
{
foreach (Object objValue in
result.Properties[propname])
{
myRow = myTable.NewRow();
myRow[0] = propname;
myRow[1] = objValue.ToString();
myTable.Rows.Add(myRow);
}
}
return myTable;
}
else
{
return null;
}
}


<!--End of Page -->


Any help would greatly appreciated!


Thanks,


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top