Search code examples
c#sharepoint-2010

How to populate HTML table with SPQuery result


I've just started playing with developing in SP2010, so I'm complete begginer . I've created simple console application with code like this :

using (SPSite currentSite = new SPSite("http://win-e9840laitme"))
{
    using (SPWeb currentWeb = currentSite.OpenWeb())
    {
        SPList myList = currentWeb.Lists["List1"];

        string currentUserName = currentWeb.CurrentUser.Name;

        SPQuery queryInformationAboutCurrentUser = new SPQuery();
        queryInformationAboutCurrentUser.Query = "<Where>" +
                                                  "<Eq><FieldRef Name='EmployeeName'/><Value Type='Text'>" + currentUserName + "</Value></Eq>" +
                                                  "</Where>";

        List<EmployeeInfo> listEmployeeInfo = new List<EmployeeInfo>();
        SPListItemCollection collectionEmployee = myList.GetItems(queryInformationAboutCurrentUser);

        foreach (SPListItem info in collectionEmployee)
        {
            EmployeeInfo eInfo = new EmployeeInfo();
            eInfo.Deparment = info["Office"].ToString();
            listEmployeeInfo.Add(eInfo);

        }
        foreach (EmployeeInfo eI in listEmployeeInfo)
        {
            SPQuery querySameOffice = new SPQuery();
            querySameOffice.Query = "<Where>" +                                                           
                                    "<Eq><FieldRef Name='Office'/><Value Type='Choice'>" + eI.Deparment + "</Value></Eq>" +                                                           
                                    "</Where>";
            SPListItemCollection collectionEmployeeDisplay = myList.GetItems(querySameOffice);
            foreach (SPListItem item in collectionEmployeeDisplay)
            {
                Console.WriteLine(item["EmployeeName"].ToString() + " " + item["PhoneNumber"].ToString() + "\n");
            }
        }
    }
}

Now I want to use that code inside sharepoint project , and instead of putting result of SPQuery on Console , I want to populate HTML table with the result staticly(without JS or Jquery , if it is possible). I've created sp project and added ApplicationPage. My idea was to use StringBuilder and something like this :

StringBuilder html = new StringBuilder();


html.Append("<table border = '1'>");
//Building the Header row.
html.Append("<tr>");            
html.Append("<th>");
html.Append("EmployeeName");
html.Append("</th>");
html.Append("<th>");
html.Append("PhoneNumber");
html.Append("</th>");
html.Append("</tr>");

and in the last foreach loop this code :

foreach (SPListItem item in collectionEmployeeDisplay)
{
    html.Append("<tr>");
    html.Append("<td>");
    html.Append(item["EmployeeName"].ToString());
    html.Append("</td>");

    html.Append("<td>");
    html.Append(item["PhoneBook"].ToString());
    html.Append("</td>");

    html.Append("</tr>");
}

//Table end.
html.Append("</table>");
//Append the HTML string to Placeholder.
PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });

I beleive there are more elegant solutions to this problem, and if you now it please give me idea .Thank you.


Solution

  • I've found a solution.First we need to add Repeater into PlaceHolderMain :

    <asp:Repeater ID="rptEmployees" runat="server">
        <HeaderTemplate>
            <table border="1" style="width:70%">
                <tr>
                    <td>Employee Name</td>
                    <td>Phone Number</td>
                    <td>Position</td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%# Eval("EmployeeName") %></td>
                <td><%# Eval("PhoneNumber") %></td>
                <td><%# Eval("Position") %></td>
            </tr>            
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    
    
    </asp:Repeater>
    

    than create another class and add this code into final loop:

    foreach (SPListItem item in collectionEmployeeDisplay)
                        {
                            DisplayEmployee dE = new DisplayEmployee();
                            dE.EmployeeName = item[Sol.PB1.Fields.EmployeeName].ToString();
                            dE.PhoneNumber = item[Sol.PB1.Fields.PhoneNumber].ToString();
                            dE.Position = item[Sol.PB1.Fields.Position].ToString();
                            display.Add(dE);
                        }
    
                        rptEmployees.DataSource = display;
                        rptEmployees.DataBind();
                    }