How can I make a list with all Tasks? It is possible to use the MyTasks webpart, this is after changing a list of all task within the sitecollection.

But i want a list of all tasks of multiple sitecollections,a global my task list.

4 Answers 4


try this:

SPSiteDataQuery q = new SPSiteDataQuery(); 

        q.ViewFields = "<FieldRef Name='Title'/><FieldRef Name='Priority'/><FieldRef Name='Status'/>";

        q.Webs = "<Webs Scope='SiteCollection' />"; 

        q.Lists = "<Lists BaseType='0' />"; 

        q.Query = "<Where><Gt><FieldRef Name='ID' /><Value Type='Number'>0</Value></Gt></Where>";

        DataTable results = new DataTable(); 

        using (SPSite site = new SPSite("http://sharepoint")) 


            using (SPWeb web = site.OpenWeb("subsite")) 


                results = web.GetSiteData(q); 
                SPList newList = null;
                Guid newListGuid = web.Lists.Add("AllTaskList", "Show all task for user from all sites", SPListTemplateType.GenericList);
                newList = web.Lists[newListGuid];

                 foreach (DataColumn dc in results.Columns)
                      newList.Fields.Add(dc.ColumnName, SPFieldType.Text, false);

                SPListItem newItem = null;
                foreach (DataRow row in dt.Rows)
                     newItem = newList.AddItem();

                     foreach (DataColumn dc in dt.Columns)
                         newItem[dc.ColumnName] = row[dc].ToString();




the code above would need some tweaking to get what you want but the bare bones are there to get you more than going! I dont think I need to explain it as its self explanitory!... query to get all tasks from site collection, add it to a datatable, from the datatable create a list, get all items from the datatable and populate the new list.... if you want to populate a current list its even easier but ill leave that up to you ;) .



if you want to get all task list from a webapplication that contains all the site collections you can use:

using (SPSite stsite = new SPSite(SPContext.Current.Web.Url))
 SPWebApplication spWebApp = stsite.WebApplication;
 foreach (SPSiteCollection spColl in spWebApp.Sites)
  foreach (SPSite spSit in spColl)
   using (SPWeb siteWeb = spSit.OpenWeb())
    SPList listSiteSetup1 = siteWeb.Lists["Tasks"];
     Some code goes here

//Gets the collection of all "Task" lists that are contained in the WebSite.
SPList listSiteSetup = SPContext.Current.Web.Lists["Tasks"];

//Get the datatable version of tasks.
SPListItemCollection items = listSiteSetup.Items;
DataTable dtTasks = items.GetDataTable();
DataColumn dtLink = new DataColumn("link");

//for (int i = 0; i <= dtTasks.Columns.Count; i++)
// string name = dtTasks.Columns[i].ColumnName;


dtTasks.Columns.Add(dtLink);//Columns to Display

string[] link = new string[1];
link[0] = "link";

BoundField bfPriority = new BoundField();
bfPriority.HeaderText = "Priority";
bfPriority.DataField = "Priority";

HyperLinkField hlfTitle = new HyperLinkField();
hlfTitle.DataTextField = "Title";
hlfTitle.HeaderText = "Title";
hlfTitle.DataNavigateUrlFields = link;

BoundField bfStatus = new BoundField();
bfStatus.HeaderText = "Status";
bfStatus.DataField = "Status";

BoundField bfStartDate = new BoundField();
bfStartDate.DataField = "StartDate";
bfStartDate.HeaderText = "Start Date";

BoundField bfDueDate = new BoundField();
bfDueDate.HeaderText = "Due Date";
bfDueDate.DataField = "DueDate";

BoundField bfAssignedTo = new BoundField();
bfAssignedTo.DataField = "AssignedTo";
bfAssignedTo.HeaderText = "Assigned To";

//working but not required
//BoundField bfPercentComplete = new BoundField();
//bfPercentComplete.DataField = "PercentComplete";
//bfPercentComplete.HeaderText = "PercentComplete";


gvTasks.AutoGenerateColumns = false;

//this loop attaches the link to the hyperlinkField to the Title column of every Task
foreach (DataRow dr in dtTasks.Rows)
 dr["link"] = SPContext.Current.Web.Url + "/Lists/Tasks/DispForm.aspx?ID=" + Convert.ToString(dr["ID"]) + "&Source=" + SPContext.Current.Web.Url;

SPUser user = SPContext.Current.Web.CurrentUser;

string rowFilter = "AssignedTo = '" + user.Name + "'";
SPGroupCollection userGroupsColle = user.Groups;

foreach (SPGroup grp in userGroupsColle)
 rowFilter = rowFilter + " OR AssignedTo = '" + grp.Name + "'";
string statusFilter = null;
DataView dv = new DataView(dtTasks, rowFilter, null, DataViewRowState.CurrentRows);

switch (Status)
 case EnumStatus.NotCompleted:
   statusFilter = "Status='In Progress' OR Status='Not Started'";
 case EnumStatus.Completed:
  statusFilter = "Status='Completed'";
 case EnumStatus.InProgress:
  statusFilter = "Status='In Progress'";
 case EnumStatus.NotStarted:
  statusFilter = "Status='Not Started'";

dv = new DataView(dv.ToTable(), statusFilter, null, DataViewRowState.CurrentRows);

Label Label1 = new Label();
Label1.Text = "There are no tasks to be completed by you or your group";
gvTasks.DataSource = dv.ToTable();

if (dv.ToTable().Rows.Count == 0)
 Label1.Visible = true;
 Label1.Visible = false;


you need to put above code into a webpart



You can think of using Content Query web part and query all tasks list within your site collection.

To view multiple fields from task list you can think of creating your own XSL Stylesheet and attach it with Content Query Web Part.

You can read more about this technique in A Little More about CQWP by Ankur Chourasia

Hope this helps.

  • The question was cross site collection not within the same site collection. Jul 11, 2012 at 21:37
  • This is also possible with the default User Task list after a small configuration modification
    – Active_t
    Jul 12, 2012 at 7:44

You can always show information only from one specific site collection. Cross Site Collection Queries can only be done by using the search. You can configure a fixed keyword query for this.

  • And to round out your options, there are third party tools like the Lightning Conductor web part to do it too. Jul 11, 2012 at 17:24

It is possible to use SharePoint search as a central place to pull this information from. The challenge is that depending on your indexing schedule the information may not be as up to date as the source lists.

Another alternative is to use execute an SPSiteDataQuery against SPWeb's GetSiteData() method. The challenge here is that it may not scale well if you have a lot of site collections you need to query. Caching can help, but there are still limits.

I worked with an example of both in my book SP 2010 Business Application Blueprints and discussed the pros and cons to both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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