Share via


SPSiteDataQuery.ViewFields Property

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)

Syntax

Public Property ViewFields As String
    Get
    Set

Dim instance As SPSiteDataQuery
Dim value As String

value = instance.ViewFields

instance.ViewFields = value
public string ViewFields { get; set; }

Property Value

Type: System.String
A string containing the inner XML that specifies the view fields.

Remarks

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.

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

Examples

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.

Imports System
Imports System.Data
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("http://localhost")
         Using web As SPWeb = site.OpenWeb("/subsite/")

            Dim query As SPSiteDataQuery = 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'>"

            Dim results As DataTable = web.GetSiteData(query)

            Dim i As Integer
            For i = 0 To results.Rows.Count - 1 Step i + 1
               Console.WriteLine("{0} {1}", results.Rows(i)("FirstName"), results.Rows(i)("Title"))
            Next

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module
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();
      }
   }
}

See Also

Reference

SPSiteDataQuery Class

SPSiteDataQuery Members

Microsoft.SharePoint Namespace