By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,730 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,730 IT Pros & Developers. It's quick & easy.

Display Active Directory Users in a ListBox

P: n/a
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.Ge t("ADPath"));
// Authentication details
de.Username =
ConfigurationManager.AppSettings.Get("ADServiceAcc ount"); //DOMAIN\User

de.Password =
ConfigurationManager.AppSettings.Get("ADServiceAcc ountPassword");
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("sAMAccount Name");
DirectorySearcher.PropertiesToLoad.Add("mail");
DirectorySearcher.PropertiesToLoad.Add("displayNam e");
DirectorySearcher.PropertiesToLoad.Add("mDBStorage Quota");
DirectorySearcher.PropertiesToLoad.Add("title");
DirectorySearcher.PropertiesToLoad.Add("physicalDe liveryOfficeName");
DirectorySearcher.PropertiesToLoad.Add("telephoneN umber");
// 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

Dec 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Check the Datasource and Datamember properties. I do not see any binding
code from below maybe I miss it.

chanmm
<j.**********@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
>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.Ge t("ADPath"));
// Authentication details
de.Username =
ConfigurationManager.AppSettings.Get("ADServiceAcc ount"); //DOMAIN\User

de.Password =
ConfigurationManager.AppSettings.Get("ADServiceAcc ountPassword");
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("sAMAccount Name");
DirectorySearcher.PropertiesToLoad.Add("mail");
DirectorySearcher.PropertiesToLoad.Add("displayNam e");
DirectorySearcher.PropertiesToLoad.Add("mDBStorage Quota");
DirectorySearcher.PropertiesToLoad.Add("title");
DirectorySearcher.PropertiesToLoad.Add("physicalDe liveryOfficeName");
DirectorySearcher.PropertiesToLoad.Add("telephoneN umber");
// 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
Dec 7 '06 #2

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

Jonathan

Chan Ming Man wrote:
Check the Datasource and Datamember properties. I do not see any binding
code from below maybe I miss it.

chanmm
<j.**********@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
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.Ge t("ADPath"));
// Authentication details
de.Username =
ConfigurationManager.AppSettings.Get("ADServiceAcc ount"); //DOMAIN\User

de.Password =
ConfigurationManager.AppSettings.Get("ADServiceAcc ountPassword");
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("sAMAccount Name");
DirectorySearcher.PropertiesToLoad.Add("mail");
DirectorySearcher.PropertiesToLoad.Add("displayNam e");
DirectorySearcher.PropertiesToLoad.Add("mDBStorage Quota");
DirectorySearcher.PropertiesToLoad.Add("title");
DirectorySearcher.PropertiesToLoad.Add("physicalDe liveryOfficeName");
DirectorySearcher.PropertiesToLoad.Add("telephoneN umber");
// 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
Dec 13 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.