共用方式為


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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Property ViewFields As String
    Get
    Set
'Usage
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 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).

Remarks

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:

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

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:

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

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.

    Value

    Description

    0

    Minor versions are visible to readers, authors and approvers.

    1

    Minor versions are visible to authors and approvers.

    2

    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:

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

Examples

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.

Imports System
Imports System.Data
Imports Microsoft.SharePoint

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

            Dim query As SPSiteDataQuery = 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.
            Dim results As DataTable = web.GetSiteData(query)

            ' Print the results.
            Console.WriteLine("{0, -30} {1, -30} {2}", "Task", "Assigned to", "% Complete")

            Dim row As DataRow
            For Each row In results.Rows

               ' Get the task name.
               Dim task As String = row("Title").ToString()

               ' Parse out the user's login name.
               Dim loginName As String = String.Empty
               Dim str() As String = row("AssignedTo").ToString().Split("#"c)
               If (str.Length > 1) Then
                  loginName = str(1)
               End If

               ' Get the percent complete.
               Dim percent As Decimal
               Dim hasValue As Boolean = _
                   Decimal.TryParse(CType(row("PercentComplete"), String), percent)
               If Not hasValue Then
                  percent = 0
               End If

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

         End Using
      End Using
      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("https://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);
               }
            }
         }
         Console.ReadLine();
      }
   }
}

See Also

Reference

SPSiteDataQuery Class

SPSiteDataQuery Members

Microsoft.SharePoint Namespace