SPSiteDataQuery.Query property
取得或設定內部 XML 定義查詢。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Property Query As String
Get
Set
'用途
Dim instance As SPSiteDataQuery
Dim value As String
value = instance.Query
instance.Query = value
public string Query { get; set; }
Property value
Type: System.String
包含片段共同作業應用程式標記語言定義的查詢字串。
備註
此屬性的值會對應至在 caml (英文),不包括結尾的開頭和結尾<Query></Query>標記Query元素的內部 XML。
查詢字串的Where元素指定決定哪些項目會傳回結果集中的篩選。下列的語法為語法用於SPQuery物件的Query屬性相同。
查詢字串的OrderBy元素會指定傳回的結果集的項目排序順序。下列的語法SPQuery物件, Query屬性所使用的語法相同,但此外也支援ListProperty和ProjectProperty子元素。如需這些子元素的說明,請參閱 < ViewFields屬性。
不包含欄位中的查詢字串的Where或OrderBy元素參照的項目並不包含在結果集。
下列的標記會顯示已格式化以方便閱讀的範例查詢字串。
<Where>
<Eq>
<FieldRef Name="AssignedTo"/>
<Value Type="Integer">
<UserID/>
</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name="Priority"/>
<ProjectProperty Name="Title" />
<ListProperty Name="Title" />
</OrderBy>
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()
' Get IDs for all fields used in the query.
Dim assignedToId As String = SPBuiltInFieldId.AssignedTo.ToString("B")
Dim taskDueDateId As String = SPBuiltInFieldId.TaskDueDate.ToString("B")
Dim titleId As String = SPBuiltInFieldId.Title.ToString("B")
Dim taskStatusId As String = SPBuiltInFieldId.TaskStatus.ToString("B")
Dim percentCompleteId As String = SPBuiltInFieldId.PercentComplete.ToString("B")
' Define the data selection.
Dim where As String = "<Where><Eq>"
where += "<FieldRef ID='" + assignedToId + "' />"
where += "<Value Type='Integer'><UserID/></Value>"
where += "</Eq></Where>"
' Define the sort order.
Dim orderBy As String = "<OrderBy>"
orderBy += "<FieldRef ID='" + taskDueDateId + "' />"
orderBy += "</OrderBy>"
' Set the query string.
query.Query = where + orderBy
' Query task lists.
query.Lists = "<Lists ServerTemplate='107'/>"
' Specify the view fields.
query.ViewFields = "<FieldRef ID='" + titleId + "' />"
query.ViewFields += "<FieldRef ID='" + taskDueDateId + "' Nullable='TRUE' />"
query.ViewFields += "<FieldRef ID='" + taskStatusId + "' Nullable='TRUE' />"
query.ViewFields += "<FieldRef ID='" + percentCompleteId + "' Nullable='TRUE' />"
' Query all Web sites in this site collection.
query.Webs = "<Webs Scope='SiteCollection'>"
' Run the query.
Dim results As DataTable = web.GetSiteData(query)
' Print the results.
Console.WriteLine("{0, -10} {1, -30} {2, -30} {3}", "Date Due", "Task", "Status", "% Complete")
Dim row As DataRow
For Each row In results.Rows
' Extract column values from the data table.
Dim dueDate As String = CType(row(taskDueDateId), String)
Dim task As String = CType(row(titleId), String)
Dim status As String = CType(row(taskStatusId), String)
Dim percentComplete As String = CType(row(percentCompleteId), String)
' Convert the due date to a short date.
Dim dt As DateTime
Dim hasDate As Boolean = DateTime.TryParse(dueDate, dt)
If hasDate Then
dueDate = dt.ToShortDateString()
Else
dueDate = String.Empty
End If
' Convert the PercentComplete field value to a percentage.
Dim pct As Decimal
Dim hasValue As Boolean = Decimal.TryParse(percentComplete, pct)
If hasValue Then
percentComplete = pct.ToString("P0")
Else
percentComplete = "0 %"
End If
' Print a line.
Console.WriteLine("{0, -10} {1, -30} {2, -30} {3, 10}", dueDate, task, status, percentComplete)
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();
// Get IDs for all fields used in the query.
string assignedToId = SPBuiltInFieldId.AssignedTo.ToString("B");
string taskDueDateId = SPBuiltInFieldId.TaskDueDate.ToString("B");
string titleId = SPBuiltInFieldId.Title.ToString("B");
string taskStatusId = SPBuiltInFieldId.TaskStatus.ToString("B");
string percentCompleteId = SPBuiltInFieldId.PercentComplete.ToString("B");
// Define the data selection.
string where = "<Where><Eq>";
where += "<FieldRef ID='" + assignedToId + "' />";
where += "<Value Type='Integer'><UserID/></Value>";
where += "</Eq></Where>";
// Define the sort order.
string orderBy = "<OrderBy>";
orderBy += "<FieldRef ID='" + taskDueDateId + "' />";
orderBy += "</OrderBy>";
// Set the query string.
query.Query = where + orderBy;
// Query task lists.
query.Lists = "<Lists ServerTemplate='107'/>";
// Specify the view fields.
query.ViewFields = "<FieldRef ID='" + titleId + "' />";
query.ViewFields += "<FieldRef ID='" + taskDueDateId + "' Nullable='TRUE' />";
query.ViewFields += "<FieldRef ID='" + taskStatusId + "' Nullable='TRUE' />";
query.ViewFields += "<FieldRef ID='" + percentCompleteId + "' Nullable='TRUE' />";
// Query all Web sites in this site collection.
query.Webs = "<Webs Scope='SiteCollection'>";
// Run the query.
DataTable results = web.GetSiteData(query);
// Print the results.
Console.WriteLine("{0, -10} {1, -30} {2, -30} {3}", "Date Due", "Task", "Status", "% Complete");
foreach (DataRow row in results.Rows)
{
// Extract column values from the data table.
string dueDate = (string)row[taskDueDateId];
string task = (string)row[titleId];
string status = (string)row[taskStatusId];
string percentComplete = (string)row[percentCompleteId];
// Convert the due date to a short date string.
DateTime dt;
bool hasDate = DateTime.TryParse(dueDate, out dt);
if (hasDate)
dueDate = dt.ToShortDateString();
else
dueDate = String.Empty;
// Convert the PercentComplete field value to a percentage.
decimal pct;
bool hasValue = decimal.TryParse(percentComplete, out pct);
if (hasValue)
percentComplete = pct.ToString("P0");
else
percentComplete = "0 %";
// Print a line.
Console.WriteLine("{0, -10} {1, -30} {2, -30} {3, 10}", dueDate, task, status, percentComplete);
}
}
}
Console.ReadLine();
}
}
}