Export (0) Print
Expand All

SPSiteDataQuery.ViewFields Property

Windows SharePoint Services 3

Gets or sets the inner XML that describes 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 containing the inner XML that specifies the view fields.

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

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 will 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 (not supported for Lookup and Person/Group fields). Example:

[xml]

<FieldRef Name="Title" 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:

Value

Description

Title

The title of the list that contains the item.

ListId

The GUID of the list that contains the item.

DraftVersionVisibility

Whether minor versions are visible on the list that contains the item.

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

Value

Description

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 will return the item title, the containing list’s title, and the containing Web site’s title.

SPSiteDataQuery query = new SPSiteDataQuery();
query.ViewFields = "<FieldRef Name=\"Title\" />" + 
                   "<ProjectProperty Name=\"Title\" />" + 
                   "<ListProperty Name=\"Title\" />";

The following example is a console application that retrieves data from all lists that are based on the Contacts list template anywhere within the current site collection.

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("/subsite/"))
            {
               SPSiteDataQuery query = new SPSiteDataQuery();

               //Ask for all lists created from the contacts template.
               query.Lists = "<Lists ServerTemplate=\"105\" />";

               // Get the Title (Last Name) and FirstName fields.
               query.ViewFields = "<FieldRef Name=\"Title\" />" + 
                                  "<FieldRef Name=\"FirstName\" Nullable=\"TRUE\"/>";

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

               DataTable results = web.GetSiteData(query);

               for (int i = 0; i < results.Rows.Count; i++)
               {
                  Console.WriteLine("{0} {1}", results.Rows[i]["FirstName"], results.Rows[i]["Title"]);
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

Community Additions

ADD
Show:
© 2014 Microsoft