SPSiteDataQuery.ViewFields property
取得或設定指定查詢中使用的檢視欄位的內部 XML。
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
包含在指定的檢視欄位的共同作業應用程式標記語言片段字串。這個字串會對應至內Collaborative Application Markup Language (CAML)ViewFields元素的內部 XML。
備註
每個欄位都是以FieldRef標籤來表示。使用Name或ID屬性來識別欄位。
FieldRef標籤具有選用Type屬性,可用來指定之欄位的資料類型。設定屬性時,視為查詢所宣告是指定類型的欄位的清單。範例:
<ViewFields>
<FieldRef Name="Title" Type="Text" />
<FieldRef Name="PercentComplete" Type="Number" />
</ViewFields>
根據預設,如果包含在查詢的清單不包含其中一個欄位中ViewFields標籤中,指定該清單中的任何項目會不出現在結果中。若要傳回空值的欄位中不包含該欄位的清單項目,將Nullable屬性設TRUE上該FieldRef標記。( 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>
Examples
下列範例中是列出網站集合中的及填入結果集中的資料從三個檢視欄位會查詢所有工作主控台應用程式。之後擷取資料,將應用程式會列印報表主控台。
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();
}
}
}