Home All Groups Group Topic Archive Search About

Need help with GridView and LDAP/GC

Author
21 Feb 2006 12:50 PM
rufpirat@gmail.com
Hello

I'm in the middle of trying to build an "AD phone book", and this being
my first try at asp.net, I have a few questions that I hope some of you
might be able to help with:

   1. Is it correct, that PageSize equals the max size of the result
set?
   2. Is there a way to make asp cache the search result, so the domain
controller won't be to bother by all the lookups, and also to speed up
searching?
   3. I've been trying to make a "more info" button, that when clicked,
should result in a lookup in the AD, for additional information about
the person (e.g. I could do the search with GC, and then lookup more
info with LDAP), however, I've been unable to figure out how to see,
what button was clicked (there is a "more info" button in each row). I
small hint would make my day!
   4. I have also had a hard time figuring out how to make my GridView
control sort the search result. I would basically like to be able to
sort each column alphabetically. Help me out :)

My code is included below; any input on it is also very welcome.

Thanks and best regards, Egil.

PS: I've put the code online in a seperate text file as well, it might
be easier to read from there:
http://www.assimilated.dk/temp/adphonebook_code.txt

----------------------------------------------------------------------------------------
HTML File (header removed, no none standard things in that):
----------------------------------------------------------------------------------------
<body>
    <form id="form1" runat="server">
    <div id="divContent">
        <h1>Search for people</h1>
        <asp:TextBox ID="txtSearch" runat="server" ToolTip="Enter
search criteria here" Width="250px" Wrap="False"
TabIndex="1"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server"
OnClick="btnSearch_Click" Text="Search" />
        <br />
        <br />
        <asp:Label ID="lblSearchStatus" runat="server" Font-Bold="True"
Visible="False"></asp:Label>
        <br />
        <br />
        <asp:GridView ID="gvSearchResult" runat="server"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
HorizontalAlign="Center">
            <Columns>
                <asp:BoundField DataField="LogonName" HeaderText="Short
name:" />
                <asp:BoundField DataField="FullName" HeaderText="Name:"
NullDisplayText="n/a" />
                <asp:BoundField DataField="Phone" HeaderText="Phone:"
/>
                <asp:BoundField DataField="Mobile" HeaderText="Mobile:"
NullDisplayText="n/a" />
                <asp:BoundField DataField="Email" HeaderText="Email:"
HtmlEncode="False" />
                <asp:BoundField DataField="Office" HeaderText="Office:"
/>
                <asp:BoundField DataField="Department"
HeaderText="Department:" />
                <asp:ButtonField ButtonType="Button"
DataTextField="MoreInfo" HeaderText="More info:" Text="More..." />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Label ID="lblMore" runat="server" Text=""
Visible="false"></asp:Label>
    </div>
    </form>
</body>

----------------------------------------------------------------------------------------
Code behind file:
----------------------------------------------------------------------------------------

public partial class SearchForPeople : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
    }

    protected void btnSearch_Click(object sender, EventArgs e) {
        DirectoryEntry searchRoot = new
DirectoryEntry("LDAP://dev-srv-01/DC=dev,DC=local","dev.local\\administrator",

"pass",AuthenticationTypes.Secure);

        // We are responsible to dispose this!
        using (searchRoot) {
            // Build the search filter.
            string searchFilter =
"(&(objectClass=person)(objectClass=user)";
            // Add search string if specified.
            if (txtSearch.Text != "")
                searchFilter += "(cn=*" + txtSearch.Text + "*))";
            else searchFilter += ")";

            // Instantiate ds object.
            DirectorySearcher ds = new DirectorySearcher(searchRoot,
searchFilter);

            // Enable paging for large queries
            ds.PageSize = 500;

            using(SearchResultCollection searchResults = ds.FindAll())
{

                if(searchResults.Count > 0) {
                    // make the People table and its columns
                    DataTable tblPeople = new DataTable("People");

                    // Holds the columns and rows as they are being
added.
                    DataColumn col;
                    DataRow row;

                    // Create LogonName column
                    col = new DataColumn("LogonName",
System.Type.GetType("System.String"));
                    col.DefaultValue = "";
                    tblPeople.Columns.Add(col);

                    // Create FullName column
                    col = new DataColumn("FullName",
System.Type.GetType("System.String"));
                    col.DefaultValue = "";
                    tblPeople.Columns.Add(col);

                    // Create Phone column
                    col = new DataColumn("Phone",
System.Type.GetType("System.String"));
                    col.DefaultValue = "";
                    tblPeople.Columns.Add(col);

                    // Create Mobile column
                    col = new DataColumn("Mobile",
System.Type.GetType("System.String"));
                    col.DefaultValue = "";
                    tblPeople.Columns.Add(col);

                    // Create Email column
                    col = new DataColumn("Email",
System.Type.GetType("System.String"));
                    col.DefaultValue = "";
                    tblPeople.Columns.Add(col);

                    // Create Office column
                    col = new DataColumn("Office",
System.Type.GetType("System.String"));
                    col.DefaultValue = "";
                    tblPeople.Columns.Add(col);

                    // Create Department column
                    col = new DataColumn("Department",
System.Type.GetType("System.String"));
                    col.DefaultValue = "";
                    tblPeople.Columns.Add(col);

                    // Create More info column
                    col = new DataColumn("MoreInfo");
                    tblPeople.Columns.Add(col);

                    // Iterate over all the results in the resultset.
                    foreach(SearchResult result in searchResults) {
                        row = tblPeople.NewRow();
                        // Getting values
                        // Display Name

if(result.Properties.Contains("samaccountname"))
                            row["LogonName"] =
result.Properties["samaccountname"][0].ToString();

                        // Display Name
                        if(result.Properties.Contains("displayName"))
                            row["FullName"] =
result.Properties["displayName"][0].ToString();

                        // Telephone number

if(result.Properties.Contains("telephoneNumber"))
                            row["Phone"] =
result.Properties["telephoneNumber"][0].ToString();

                        // Mobile phone
                        if(result.Properties.Contains("mobile"))
                            row["Mobile"] =
result.Properties["mobile"][0].ToString();

                        // Email addresss (and format it)
                        if(result.Properties.Contains("mail"))
                            if(result.Properties["mail"][0].ToString()
!= "") {
                                string email =
result.Properties["mail"][0].ToString();
                                row["Email"] = "<a href=\"mailto:" +
email + "\" title=\"Send mail to " + email + "\">" + email + "</a>";
                            }

                        // Office location

if(result.Properties.Contains("physicalDeliveryOfficeName"))
                            row["Office"] =
result.Properties["physicalDeliveryOfficeName"][0].ToString();

                        // Department
                        if(result.Properties.Contains("department"))
                            row["Department"] =
result.Properties["department"][0].ToString();

                        // If there is actually something to display,
create a new table row.
                        if(row["Phone"] != "" || row["Mobile"] != "" ||
row["Email"] != "" || row["Office"] != "" || row["Department"] != "") {
                            tblPeople.Rows.Add(row);
                        }
                    }

                    // instantiate a new DataSet object that
                    DataSet dataSet = new DataSet();
                    dataSet.Tables.Add(tblPeople);

                    // set the data source for the grid to the people
table
                    gvSearchResult.DataSource =
dataSet.Tables["People"];
                    gvSearchResult.DataBind();

                    lblSearchStatus.Visible = true;
                    lblSearchStatus.Text =
tblPeople.Rows.Count.ToString() + " matches found.";
                } else {
                    lblSearchStatus.Visible = true;
                    lblSearchStatus.Text = "No matches found...";
                }
            }
        }
    }
}

----------------------------------------------------------------------------------------

Author
22 Feb 2006 3:17 PM
DWS
Rufpirat,
First off don't ever ask or even think about caching personal information
again. Ok?  Try taking the first wildcard out of your searh to make it go
faster.  Make a second **wildcard search available when the exact result is
not found.  You didn't provide details on your domain server but you probably
can make an indexed table and store it in SQL server for speed.  Use the
domain to update the table when changes occur.  SQL Server will safely cache
data for you.

Good Luck
DWS


Show quoteHide quote
"rufpi***@gmail.com" wrote:

> Hello
>
> I'm in the middle of trying to build an "AD phone book", and this being
> my first try at asp.net, I have a few questions that I hope some of you
> might be able to help with:
>
>    1. Is it correct, that PageSize equals the max size of the result
> set?
>    2. Is there a way to make asp cache the search result, so the domain
> controller won't be to bother by all the lookups, and also to speed up
> searching?
>    3. I've been trying to make a "more info" button, that when clicked,
> should result in a lookup in the AD, for additional information about
> the person (e.g. I could do the search with GC, and then lookup more
> info with LDAP), however, I've been unable to figure out how to see,
> what button was clicked (there is a "more info" button in each row). I
> small hint would make my day!
>    4. I have also had a hard time figuring out how to make my GridView
> control sort the search result. I would basically like to be able to
> sort each column alphabetically. Help me out :)
>
> My code is included below; any input on it is also very welcome.
>
> Thanks and best regards, Egil.
>
> PS: I've put the code online in a seperate text file as well, it might
> be easier to read from there:
> http://www.assimilated.dk/temp/adphonebook_code.txt
>
> ----------------------------------------------------------------------------------------
> HTML File (header removed, no none standard things in that):
> ----------------------------------------------------------------------------------------
> <body>
>     <form id="form1" runat="server">
>     <div id="divContent">
>         <h1>Search for people</h1>
>         <asp:TextBox ID="txtSearch" runat="server" ToolTip="Enter
> search criteria here" Width="250px" Wrap="False"
> TabIndex="1"></asp:TextBox>
>         <asp:Button ID="btnSearch" runat="server"
> OnClick="btnSearch_Click" Text="Search" />
>         <br />
>         <br />
>         <asp:Label ID="lblSearchStatus" runat="server" Font-Bold="True"
> Visible="False"></asp:Label>
>         <br />
>         <br />
>         <asp:GridView ID="gvSearchResult" runat="server"
> AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
> HorizontalAlign="Center">
>             <Columns>
>                 <asp:BoundField DataField="LogonName" HeaderText="Short
> name:" />
>                 <asp:BoundField DataField="FullName" HeaderText="Name:"
> NullDisplayText="n/a" />
>                 <asp:BoundField DataField="Phone" HeaderText="Phone:"
> />
>                 <asp:BoundField DataField="Mobile" HeaderText="Mobile:"
> NullDisplayText="n/a" />
>                 <asp:BoundField DataField="Email" HeaderText="Email:"
> HtmlEncode="False" />
>                 <asp:BoundField DataField="Office" HeaderText="Office:"
> />
>                 <asp:BoundField DataField="Department"
> HeaderText="Department:" />
>                 <asp:ButtonField ButtonType="Button"
> DataTextField="MoreInfo" HeaderText="More info:" Text="More..." />
>             </Columns>
>         </asp:GridView>
>         <br />
>         <asp:Label ID="lblMore" runat="server" Text=""
> Visible="false"></asp:Label>
>     </div>
>     </form>
> </body>
>
> ----------------------------------------------------------------------------------------
> Code behind file:
> ----------------------------------------------------------------------------------------
>
> public partial class SearchForPeople : System.Web.UI.Page
> {
>     protected void Page_Load(object sender, EventArgs e) {
>     }
>
>     protected void btnSearch_Click(object sender, EventArgs e) {
>         DirectoryEntry searchRoot = new
> DirectoryEntry("LDAP://dev-srv-01/DC=dev,DC=local","dev.local\\administrator",
>
> "pass",AuthenticationTypes.Secure);
>
>         // We are responsible to dispose this!
>         using (searchRoot) {
>             // Build the search filter.
>             string searchFilter =
> "(&(objectClass=person)(objectClass=user)";
>             // Add search string if specified.
>             if (txtSearch.Text != "")
>                 searchFilter += "(cn=*" + txtSearch.Text + "*))";
>             else searchFilter += ")";
>
>             // Instantiate ds object.
>             DirectorySearcher ds = new DirectorySearcher(searchRoot,
> searchFilter);
>
>             // Enable paging for large queries
>             ds.PageSize = 500;
>
>             using(SearchResultCollection searchResults = ds.FindAll())
> {
>
>                 if(searchResults.Count > 0) {
>                     // make the People table and its columns
>                     DataTable tblPeople = new DataTable("People");
>
>                     // Holds the columns and rows as they are being
> added.
>                     DataColumn col;
>                     DataRow row;
>
>                     // Create LogonName column
>                     col = new DataColumn("LogonName",
> System.Type.GetType("System.String"));
>                     col.DefaultValue = "";
>                     tblPeople.Columns.Add(col);
>
>                     // Create FullName column
>                     col = new DataColumn("FullName",
> System.Type.GetType("System.String"));
>                     col.DefaultValue = "";
>                     tblPeople.Columns.Add(col);
>
>                     // Create Phone column
>                     col = new DataColumn("Phone",
> System.Type.GetType("System.String"));
>                     col.DefaultValue = "";
>                     tblPeople.Columns.Add(col);
>
>                     // Create Mobile column
>                     col = new DataColumn("Mobile",
> System.Type.GetType("System.String"));
>                     col.DefaultValue = "";
>                     tblPeople.Columns.Add(col);
>
>                     // Create Email column
>                     col = new DataColumn("Email",
> System.Type.GetType("System.String"));
>                     col.DefaultValue = "";
>                     tblPeople.Columns.Add(col);
>
>                     // Create Office column
>                     col = new DataColumn("Office",
> System.Type.GetType("System.String"));
>                     col.DefaultValue = "";
>                     tblPeople.Columns.Add(col);
>
>                     // Create Department column
>                     col = new DataColumn("Department",
> System.Type.GetType("System.String"));
>                     col.DefaultValue = "";
>                     tblPeople.Columns.Add(col);
>
>                     // Create More info column
>                     col = new DataColumn("MoreInfo");
>                     tblPeople.Columns.Add(col);
>
>                     // Iterate over all the results in the resultset.
>                     foreach(SearchResult result in searchResults) {
>                         row = tblPeople.NewRow();
>                         // Getting values
>                         // Display Name
>
> if(result.Properties.Contains("samaccountname"))
>                             row["LogonName"] =
> result.Properties["samaccountname"][0].ToString();
>
>                         // Display Name
>                         if(result.Properties.Contains("displayName"))
>                             row["FullName"] =
> result.Properties["displayName"][0].ToString();
>
>                         // Telephone number
>
> if(result.Properties.Contains("telephoneNumber"))
>                             row["Phone"] =
> result.Properties["telephoneNumber"][0].ToString();
>
>                         // Mobile phone
>                         if(result.Properties.Contains("mobile"))
>                             row["Mobile"] =
> result.Properties["mobile"][0].ToString();
>
>                         // Email addresss (and format it)
>                         if(result.Properties.Contains("mail"))
>                             if(result.Properties["mail"][0].ToString()
> != "") {
>                                 string email =
> result.Properties["mail"][0].ToString();
>                                 row["Email"] = "<a href=\"mailto:" +
> email + "\" title=\"Send mail to " + email + "\">" + email + "</a>";
>                             }
>
>                         // Office location
>
> if(result.Properties.Contains("physicalDeliveryOfficeName"))
>                             row["Office"] =
> result.Properties["physicalDeliveryOfficeName"][0].ToString();
>
>                         // Department
>                         if(result.Properties.Contains("department"))
>                             row["Department"] =
> result.Properties["department"][0].ToString();
>
>                         // If there is actually something to display,
> create a new table row.
>                         if(row["Phone"] != "" || row["Mobile"] != "" ||
> row["Email"] != "" || row["Office"] != "" || row["Department"] != "") {
>                             tblPeople.Rows.Add(row);
>                         }
>                     }
>
>                     // instantiate a new DataSet object that
>                     DataSet dataSet = new DataSet();
>                     dataSet.Tables.Add(tblPeople);
>
>                     // set the data source for the grid to the people
> table
>                     gvSearchResult.DataSource =
> dataSet.Tables["People"];
>                     gvSearchResult.DataBind();
>
>                     lblSearchStatus.Visible = true;
>                     lblSearchStatus.Text =
> tblPeople.Rows.Count.ToString() + " matches found.";
>                 } else {
>                     lblSearchStatus.Visible = true;
>                     lblSearchStatus.Text = "No matches found...";
>                 }
>             }
>         }
>     }
> }
>
> ----------------------------------------------------------------------------------------
>
>
Are all your drivers up to date? click for free checkup

Author
22 Feb 2006 7:57 PM
rufpirat@gmail.com
Hi DWS

Thanks for the input, I'll keep personal information uncached from now
on ;)

The domain is a Active Directory, running Windows 2000 Native Mode.
Anyway, I think I'll leave the searching code as it is atm., and if the
lookup time becomes a issue (it doesn't seem to be atm.), i'll inprove
on it.

My main concern right now is the presentation issue's i'm having
(question 3 and 4). Again, thanks for the input.

Regards, Egil.

Bookmark and Share