SPSiteDataQuery.ViewFields property

Gets or sets the inner XML that specifies the view fields used in the query.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public string ViewFields { get; set; }

Property value

Type: System.String
A string that contains a fragment in Collaborative Application Markup Language that specifies the view fields. This string corresponds to the inner XML of the ViewFields element in Collaborative Application Markup Language (CAML).

Each field is represented by a FieldRef tag. Use the Name or ID attribute to identify the field.

The FieldRef tag has an optional Type attribute that can be used to specify the data type of the field. When the attribute is set, only lists that declare the field to be of the specified type are considered by the query. Example:

  <FieldRef Name="Title" Type="Text" />
  <FieldRef Name="PercentComplete" Type="Number" />

By default, if a list included in the query does not contain one of the fields specified in the ViewFields tag, no items from that list appear in the results. To return an empty value for a field on items in lists that do not contain that field, set the Nullable attribute to TRUE on that FieldRef tag. (For Lookup and User fields, you must also set the Type attribute.) Example:

  <FieldRef Name="AssignedTo" Type="User" Nullable="TRUE" />

List properties such as Title may be included in the query with the ListProperty tag. The Name attribute of the ListProperty tag identifies the specific property and may contain one of the following values:

  • Title -- the title of the list that contains the item.

  • ListId -- the GUID of the list that contains the item.

  • DraftVersionVisibility – indicates whether minor versions of documents are visible to readers, authors, or approvers.




    Minor versions are visible to readers, authors and approvers.


    Minor versions are visible to authors and approvers.


    Minor versions are visible only to approvers.

Site properties may be included by using the ProjectProperty tag. The Name attribute of the ProjectProperty tag identifies the specific property and may contain one of the following values:

  • Title -- the title of the Web site that contains the item.

  • WebId -- the GUID of the Web site that contains the item.

For each item, the following example returns the item title, the containing list’s title, and the containing Web site’s title. Example:

  <FieldRef Name="Title" />
  <ProjectProperty Name="Title" />
  <ListProperty Name="Title" />

The following example is a console application that queries all Tasks lists in a site collection and populates the result set with data from three view fields. After retrieving the data, 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();

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

               // Ask for lists created from the Tasks template.
               query.Lists = "<Lists ServerTemplate='107'/>";

               // Specify the view fields.
               query.ViewFields = "<FieldRef Name='Title' Type='Text'/>";
               query.ViewFields += "<FieldRef Name='AssignedTo' Type='User' Nullable='TRUE' />";
               query.ViewFields += "<FieldRef Name='PercentComplete' Type='Number' Nullable='TRUE' />";

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

               // Print the results.
               Console.WriteLine("{0, -30} {1, -30} {2}", "Task", "Assigned to", "% Complete");
               foreach (DataRow row in results.Rows)
                  // Get the task name.
                  string task = row["Title"].ToString();

                  // Parse out the user's login name.
                  string loginName = String.Empty;
                  string[] str = row["AssignedTo"].ToString().Split('#');
                  if (str.Length > 1) loginName = str[1];

                  // Get the percent complete.
                  decimal percent;
                  bool hasValue = decimal.TryParse((string)row["PercentComplete"], out percent);
                  if (!hasValue) percent = 0;

                  Console.WriteLine("{0, -30} {1, -30} {2, 10:P0}", task, loginName, percent);