SPSiteDataQuery.Query Property

Gets or sets the inner XML that defines the query.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

public string Query { get; set; }

Property Value

Type: System.String
A string that contains a fragment in Collaborative Application Markup Language that defines the query.

The value of this property corresponds to the inner XML of the Query element in CAML, excluding the opening and closing <Query></Query> tags.

The Where element of the query string specifies the filter that determines which items are returned in the result set. The syntax is the same as the syntax used for the Query property of an SPQuery object.

The OrderBy element of the query string specifies the sort order of the items returned in the result set. The syntax is the same as the syntax used for the Query property of an SPQuery object, but in addition also supports the ListProperty and ProjectProperty subelements. For a description of these subelements, see the ViewFields property.

Items that do not contain a field that is referred to in Where or OrderBy elements of the query string are not included in the result set.

The following markup shows an example query string that has been formatted to make it easier to read.

    <FieldRef Name="AssignedTo"/>
    <Value Type="Integer">
  <FieldRef Name="Priority"/>
  <ProjectProperty Name="Title" />
  <ListProperty Name="Title" />

The following example is a console application that queries all Tasks lists in a site collection. The query string selects tasks that are assigned to the current user, ordered by date due. After the results are returned, the application prints a report to the console.

using System;
using System.Data;
using Microsoft.SharePoint;

namespace Test
   class ConsoleApp
      static void Main(string[] args)
         using (SPSite site = new SPSite("http://localhost"))
            using (SPWeb web = site.OpenWeb())
               SPSiteDataQuery query = new SPSiteDataQuery();

               // Get IDs for all fields used in the query.
               string assignedToId = SPBuiltInFieldId.AssignedTo.ToString("B");
               string taskDueDateId = SPBuiltInFieldId.TaskDueDate.ToString("B");
               string titleId = SPBuiltInFieldId.Title.ToString("B");
               string taskStatusId = SPBuiltInFieldId.TaskStatus.ToString("B");
               string percentCompleteId = SPBuiltInFieldId.PercentComplete.ToString("B");

               // Define the data selection.
               string where = "<Where><Eq>";
               where += "<FieldRef ID='" + assignedToId + "' />";
               where += "<Value Type='Integer'><UserID/></Value>";
               where += "</Eq></Where>";

               // Define the sort order.
               string orderBy = "<OrderBy>";
               orderBy += "<FieldRef ID='" + taskDueDateId + "' />";
               orderBy += "</OrderBy>";

               // Set the query string.
               query.Query = where + orderBy;

               // Query task lists.
               query.Lists = "<Lists ServerTemplate='107'/>";

               // Specify the view fields.
               query.ViewFields = "<FieldRef ID='" + titleId + "' />";
               query.ViewFields += "<FieldRef ID='" + taskDueDateId + "' Nullable='TRUE' />";
               query.ViewFields += "<FieldRef ID='" + taskStatusId + "' Nullable='TRUE' />";
               query.ViewFields += "<FieldRef ID='" + percentCompleteId + "' Nullable='TRUE' />";

               // Query all Web sites in this site collection.
               query.Webs = "<Webs Scope='SiteCollection'>";

               // Run the query.
               DataTable results = web.GetSiteData(query);

               // Print the results.
               Console.WriteLine("{0, -10} {1, -30} {2, -30} {3}", "Date Due", "Task", "Status", "% Complete");
               foreach (DataRow row in results.Rows)
                  // Extract column values from the data table.
                  string dueDate = (string)row[taskDueDateId];
                  string task = (string)row[titleId];
                  string status = (string)row[taskStatusId];
                  string percentComplete = (string)row[percentCompleteId];

                  // Convert the due date to a short date string.
                  DateTime dt;
                  bool hasDate = DateTime.TryParse(dueDate, out dt);
                  if (hasDate)
                     dueDate = dt.ToShortDateString();
                     dueDate = String.Empty;

                  // Convert the PercentComplete field value to a percentage.
                  decimal pct;
                  bool hasValue = decimal.TryParse(percentComplete, out pct);
                  if (hasValue)
                     percentComplete = pct.ToString("P0");
                     percentComplete = "0 %";

                  // Print a line.
                  Console.WriteLine("{0, -10} {1, -30} {2, -30} {3, 10}", dueDate, task, status, percentComplete);