Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to loop through a table that I created using c# code and display data pulled from a sharepoint list which was formated in a table. This is the code I came up with.

    protected override void RenderContents(HtmlTextWriter output)
    {
        try
        {
            SPSite thisSite = SPControl.GetContextSite(Context);
            using (SPWeb topWeb = thisSite.OpenWeb("/"))
            {
                SPList newsList = topWeb.Lists["Headlines"];
                SPQuery query = new SPQuery();
                query.ExpandRecurrence = true;
                query.Query = NewsListCamlQuery;
                query.RowLimit = 2;
                SPListItemCollection coll = newsList.GetItems(query);

                output.Write("<table border='0' style='width: 100%;'><tbody>");

                for (int i = 0; i < 2; i++)
                {
                    output.Write("<tr>");

                    for (int j = 0; j < 3; j++)
                    {
                        output.Write("<td>");

                        foreach (SPListItem item in coll)
                        {
                            int newsID = int.Parse(item.ID.ToString());

                            output.Write("<hr/>");
                            output.Write("<table style='width: 100%; height: 143px;'><tbody>");
                            output.Write("<tr><td valign='top'><h4>");
                            output.Write(item["Title"].ToString());
                            output.Write("</h4></td></tr>");
                            output.Write("<tr><td valign='top'><h4 class='ms-rteElement-H4B'><span style='font-size: 8pt;'>");
                            output.Write(item["Sub_x0020_Heading"].ToString());
                            output.Write("</span></h4></td></tr>");
                            output.Write("<tr><td valign='top'><span style='font-size: 8pt;'>");
                            output.Write("<img class='image' src='");
                            //output.Write(item["Photo"].ToString());
                            output.Write("' Width='64px' Height='48px' Border='0' alt='' style='margin: 5px;'/>");
                            output.Write(item["Details"].ToString());
                            output.Write("</span></td></tr>");
                            output.Write("<tr><td valign='top'>");
                            output.Write("<a class='mt-linkMore' href='/Lists/Headlines/DispForm.aspx?ID=");
                            output.Write(newsID);
                            output.Write("' <span style='font-size: 6pt;'>Read More</span></a></td></tr>");
                            output.Write("</tbody></table>");
                        }

                        output.Write("</td>");
                    }

                    output.Write("</tr>");
                }

                output.Write("</tbody></table>");
            }
        }
        catch (Exception exception)
        {
            output.Write("Error : " + exception.Message);
        }
    }

This code is currently re-writing the same record in each cell. I would like for it to display the six records that is being pulled from the sharepoint list.

share|improve this question
    
The simplest solution to debug is to write all the html in a string and when you have completed the loop copy the data from string and paste it in a html file (text file saved as html). Then see the html and you will find where you are going wrong. –  Ehsan Jul 10 '13 at 7:27
add comment

1 Answer

if you want to display two lines of 3 cells, you should not put the foreach.

On the inside of the second loop, you can make reference directly to the element of your list:

for (int i = 0; i < 2; i++)
{
    output.Write("<tr>");

    for (int j = 0; j < 3; j++)
    {
        output.Write("<td>");

        int index;
        if (i = 0)
            index = j;
        else
            index = j + 3;

        SPListItem item = coll[index];
        int newsID = int.Parse(item.ID.ToString());
        ...
share|improve this answer
    
Where in my code should I place this line of code. Also will this allow me to retieve the last 6 rows of a sharepoint list. –  user2567387 Jul 10 '13 at 13:22
    
in the second for loop : –  Rems Jul 11 '13 at 7:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.