473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Display Active Directory Users in a ListBox

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
2 11970
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Luis Esteban Valencia | last post by:
Hello gusys, Is it possible to make my asp.net application add users to the AD , I also want to be able to delete users, modify their information, everything through a website. Thanks for the...
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
0
by: Kooki | last post by:
Hi, I am developing a small programme to add ACTIVE DIRECTORY users to another server. everything works fine .. I get the users from AD using LDAP but while adding users I need to get the user...
0
by: Kooki | last post by:
Hi, I am developing a small programme to add ACTIVE DIRECTORY users to another server. everything works fine .. I get the users from AD using LDAP but while adding users I need to get the user...
0
by: jakobsgaard | last post by:
It is possible to Map a certificate to a Active Directory User Account from DotNet? Please provide an example. Best regards, Ejnar Jakobsgaard...
1
by: Lucky | last post by:
hi guys, after long long efforts i got access to the active directory for "Intigrated windows authentication". now i', suppose to get access the network resources. the problem is i'm getting...
2
by: Jim in Arizona | last post by:
My goal, somehow, is to populate a dropdownlist with all the user names in active directory. I don't even know where to begin, really. I added a reference to System.DirectoryServices so I could...
7
by: CoolBreeze812 | last post by:
Hello, I'm using the following code to get the list of computers joined to a domain in Active Directory and using the System.DirectoryServices namespace. DirectoryEntry dirEntry = new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.