SPSiteDataQuery.ViewFields 属性

获取或设置指定在查询中使用的视图字段的内部 XML。

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
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; }

属性值

类型:System.String
一个字符串,包含在指定的视图字段的协作应用程序标记语言片段。此字符串对应于ViewFields元素中协作应用程序标记语言 (CAML)的内部 XML。

备注

由FieldRef标记表示每个字段。使用Name或ID属性标识的字段。

FieldRef标记都有一个可用于指定的字段的数据类型的可选Type属性。当设置该属性时,仅声明将指定类型的字段的列表被视为由查询。示例:

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

默认情况下,如果包含在查询的列表不包含其中一个字段在ViewFields标记中,指定该列表中的任何项目的结果中显示。若要在不包含该字段的列表中的项目返回的字段为空值,请在该FieldRef标记Nullable属性设置为TRUE 。(对于Lookup和User字段,还必须设置Type属性。)示例:

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

列出属性,如Title可能包含在与ListProperty标记查询。ListProperty标记的Name属性标识的特定属性,并且可能包含下列值之一:

  • Title -包含的项目列表的标题。

  • ListId -包含的项目列表的 GUID。

  • DraftVersionVisibility – 指示是否对读者、 作者或审批者可见的文档的次要版本。

    说明

    0

    次要版本都能看到读者、 作者和审批者。

    1

    次要版本都能看到作者和审批者。

    2

    次要版本是仅对审批者可见。

通过使用ProjectProperty标记,可能包括网站属性。ProjectProperty标记的Name属性标识的特定属性,并且可能包含下列值之一:

  • Title -包含的项目网站的标题。

  • WebId -网站包含的项目的 GUID。

对于每个项,下面的示例返回的项标题、 包含列表的标题和包含网站的标题。示例:

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

示例

下面的示例是一个查询的所有任务的控制台应用程序列出了在网站集中并填充结果从三个视图字段的数据集。检索数据之后, 将应用程序打印到控制台的报告。

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();
      }
   }
}

另请参阅

引用

SPSiteDataQuery 类

SPSiteDataQuery 成员

Microsoft.SharePoint 命名空间