Question

I took some code for displaying the content of a sharepoint 2010 list in a webpart based on a CAML query and modified it to display the same thing from two lists instead of one in a table. Now I want to further modify the code in order to get the number of incomplete tasks across both lists. to do that I added counters to the loops that populate the table and stored the result in another variable c. Now, the table is populated fine and does what i want it to do, but the value of the variable c is not displayed... can anyone help? I'll be really thankfull. Here is the code:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SharePoint365.WebParts.OutstandingTasks
{
  [ToolboxItemAttribute(false)]
  public class OutstandingTasks : WebPart
  {
    int a; // HERE (1)
    int b; // HERE (1)
    int c; // HERE (1)
    protected override void CreateChildControls()
    {
      /*int a;
      int b;
      int c;*/
      SPList taskList = SPContext.Current.Web.Lists["test list 1"];
      SPList taskList1 = SPContext.Current.Web.Lists["test list 2"];

      SPQuery query = new SPQuery();
      query.Query = "<Where><Neq><FieldRef Name='Status' />";
                   +"<Value Type='Text'>Completed</Value></Neq></Where>";
      query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Status'/>";

      SPListItemCollection tasks = taskList.GetItems(query);
      SPListItemCollection tasks1 = taskList1.GetItems(query);
      string htmlTable = "<table border='1'>";  
      htmlTable += "<tr><td>Title</td><td>Status</td></tr>";  

      foreach (SPListItem aTask in tasks)  
      {  
        htmlTable += string.Format("<tr><td>{0}</td><td>{1}</td></tr>",
                       aTask["Title"], aTask["Status"]);
        a++;  // HERE (2)
      }
      foreach (SPListItem aTask in tasks1)
      {
        htmlTable += string.Format("<tr><td>{0}</td><td>{1}</td></tr>",
                       aTask["Title"], aTask["Status"]);
        b++; // HERE (3)
      }

      htmlTable += "</table>";  

      c = a + b;            // HERE (4)
      Console.WriteLine(c); // HERE (4)

      LiteralControl tableControl = new LiteralControl(htmlTable);  
      this.Controls.Add(tableControl); 
    }
  }
}
Was it helpful?

Solution

You cant use Console.WriteLine to display c. You need to use a LiteralControl like you do with your other strings:

c = a + b;
LiteralControl totals = new LiteralControl(c.ToString());
this.Controls.Add(totals);  

OTHER TIPS

May be you got the answer but. Your approach to filter two table is wrong. You need to use SPSiteDataQuery Object which we can use to query multiple sharepoint list.

SPContext.Current.Web.GetSiteData(SPSiteDataQuery Obj);

Follow below link to know more about it http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top