Try creating tasks in several sites within the site collection, assign them to your own account then run this project.

This SPSiteDataQuery searches for all entries in all Tasks folders that are assigned to the current

Steps
  • Open Visual Studio in your system.
  • Select Console Application template and give as name.
  • Add a Microsoft.Client Assembly reference file in right side reference tab in visual studio.
  • Replace Program.cs with the source code.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.SharePoint;
  7. namespace GENERAL_CrossListQuery
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //Start by formulating the query in CAML
  14. SPSiteDataQuery query = new SPSiteDataQuery();
  15. //Get IDs for the SharePoint built-in fields we want to use
  16. string assignedToID = SPBuiltInFieldId.AssignedTo.ToString("B");
  17. string taskDueDateID = SPBuiltInFieldId.TaskDueDate.ToString("B");
  18. string titleID = SPBuiltInFieldId.Title.ToString("B");
  19. string taskStatusID = SPBuiltInFieldId.TaskStatus.ToString("B");
  20. string percentCompleteID = SPBuiltInFieldId.PercentComplete.ToString("B");
  21. //This is the selection creterion
  22. string whereClause = "<Where><Eq><FieldRef ID='" + assignedToID + "' />"
  23. + "<Value Type='Integer'><UserID/></Value>"
  24. + "</Eq></Where>";
  25. //This is the sort order
  26. string orderByClause = "<OrderBy><FieldRef ID='" + taskDueDateID + "' /></OrderBy>";
  27. //Set the query CAML
  28. query.Query = whereClause + orderByClause;
  29. //We will query all the Tasks lists
  30. query.Lists = "<Lists ServerTemplate='107' />";
  31. //Define the view fields in the result set
  32. string viewFields = "<FieldRef ID='" + titleID + "' />"
  33. + "<FieldRef ID='" + taskDueDateID + "' Nullable='TRUE' />"
  34. + "<FieldRef ID='" + taskStatusID + "' Nullable='TRUE' />"
  35. + "<FieldRef ID='" + percentCompleteID + "' Nullable='TRUE' />";
  36. query.ViewFields = viewFields;
  37. //Query all the SPWebs in this SPSite
  38. query.Webs = "<Webs Scope='SiteCollection'>";
  39. //Get the SPSite and SPWeb, ensuring correct disposal
  40. //Replace the URL with your own site collection
  41. using (SPSite site = new SPSite("http://intranet.contoso.com/"))
  42. {
  43. using (SPWeb web = site.OpenWeb())
  44. {
  45. //Run the query
  46. DataTable resultsTable = web.GetSiteData(query);
  47. //Print a heading line
  48. Console.WriteLine("{0, -10} {1, -30} {2, -20} {3}", "Date Due", "Task", "Status", "% Complete");
  49. //Loop through the results and print them
  50. foreach (DataRow currentRow in resultsTable.Rows)
  51. {
  52. //Extract various values
  53. string dueDate = (string)currentRow[taskDueDateID];
  54. string task = (string)currentRow[titleID];
  55. string status = (string)currentRow[taskStatusID];
  56. string percentComplete = (string)currentRow[percentCompleteID];
  57. //Format the due date
  58. DateTime dueDateTime;
  59. bool hasDate = DateTime.TryParse(dueDate, out dueDateTime);
  60. if (hasDate)
  61. {
  62. dueDate = dueDateTime.ToShortDateString();
  63. }
  64. else
  65. {
  66. dueDate = String.Empty;
  67. }
  68. //Format the percent complete string
  69. decimal pctDec;
  70. bool hasValue = decimal.TryParse(percentComplete, out pctDec);
  71. if (hasValue)
  72. {
  73. percentComplete = pctDec.ToString("P0");
  74. }
  75. else
  76. {
  77. percentComplete = "0%";
  78. }
  79. //Print a line for this row
  80. Console.WriteLine("{0, -10} {1, -30} {2, -20} {3, 10}", dueDate, task, status, percentComplete);
  81. }
  82. }
  83. }
  84. //Wait for the user to press a key before closing
  85. Console.ReadKey();
  86. }
  87. }
  88. }