共用方式為


SPSiteDataQuery class

代表可以在相同網站集合中的多個網站中的多個清單中執行的查詢。

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPSiteDataQuery

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public NotInheritable Class SPSiteDataQuery
'用途
Dim instance As SPSiteDataQuery
public sealed class SPSiteDataQuery

備註

您可以使用這個類別的執行個體選取清單中,或是從目前的網站集合中的所有清單擷取資料。藉由設定Webs屬性中指定查詢的範圍。指定參與藉由設定Lists屬性並由設定ViewFields屬性傳回的欄位查詢清單。藉由設定Query屬性,控制資料的選取項目及順序。

若要執行查詢, SPSiteDataQuery將物件傳遞至GetSiteData(SPSiteDataQuery)方法,這會傳回DataTable物件,其中包含代表查詢結果的資料列。DataTable.Rows集合中的每個DataRow物件代表找出符合查詢的單一項目。DataTable.Columns集合中的每個DataColumn物件代表要求ViewFields屬性中的欄位及資料行名稱會等於Name屬性,針對該欄位的值。此外的運算列表包含名為WebId,以識別包含每個項目的的網站欄、 欄名為ListId,以識別包含每個項目清單和欄名為ID,識別每個項目。

Examples

下列範例會查詢所建立的任何地方清單範本的網站集合中,會擷取第一個及最後一個的每個連絡人,並控制GridView中的資訊的顯示名稱的連絡人的所有清單網頁組件。

Note that if you compile the example code, you can deploy the Web Part simply by copying the compiled assembly to the bin directory of the Web application. If you choose that method of deployment, be sure your project includes a reference to Microsoft.SharePoint.Security.dll. Then add the Web Part to the SafeControls list in the web.config file and elevate the Web application's trust level to WSS_Medium. For more information, see Deploying Web Parts in Windows SharePoint Services and Securing Web Parts in Windows SharePoint Services.

注意

Contacts的列舉值是 105。

Imports System
Imports System.Data
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint

Public Class ContactViewer
   Inherits WebPart

   Private grid As GridView

   Protected Overrides Sub CreateChildControls()
      MyBase.CreateChildControls()

      ' Add an instance of the grid control.
      Me.grid = New GridView()
      Controls.Add(Me.grid)

   End Sub

   Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)

      Dim web As SPWeb = SPContext.Current.Web
      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' Type='Text'/>"
      ' Note that setting the Nullable attribute to TRUE
      ' causes an empty value to be returned for lists that
      ' do not include the FirstName column. The default is 
      ' to skip a list that does not include the column.

      ' Set the sort order.
      query.Query = "<OrderBy>" + _
                        "<FieldRef Name='Title' />" + _
                    "</OrderBy>"

      ' Query all Web sites in this site collection.
      query.Webs = "<Webs Scope='SiteCollection' />"

      Dim dt As DataTable = web.GetSiteData(query)
      Dim dv As DataView = New DataView(dt)

      ' Set up the field bindings.
      Dim boundField As BoundField = New BoundField()
      boundField.HeaderText = "Last Name"
      boundField.DataField = "Title"
      Me.grid.Columns.Add(boundField)

      boundField = New BoundField()
      boundField.HeaderText = "First Name"
      boundField.DataField = "FirstName"
      Me.grid.Columns.Add(boundField)

      Me.grid.AutoGenerateColumns = False
      Me.grid.DataSource = dv
      Me.grid.DataBind()

      Me.grid.AllowSorting = True
      Me.grid.HeaderStyle.Font.Bold = True

      Me.grid.RenderControl(writer)

   End Sub

End Class
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace SampleWebParts
{
   public class ContactViewer : WebPart
   {
      private GridView grid;

      protected override void CreateChildControls()
      {
         base.CreateChildControls();

         // Add an instance of the grid control.
         this.grid = new GridView();
         this.Controls.Add(this.grid);
      }

      protected override void RenderContents(HtmlTextWriter writer)
      {
         SPWeb web = SPContext.Current.Web;
         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\" Type=\"Text\"/>";
        // Note that setting the Nullable attribute to TRUE
        // causes an empty value to be returned for lists that
        // do not include the FirstName column. The default is 
        // to skip a list that does not include the column.

         // Set the sort order.
         query.Query = "<OrderBy>" + 
                           "<FieldRef Name=\"Title\" />" + 
                       "</OrderBy>";

         // Query all Web sites in this site collection.
         query.Webs = "<Webs Scope=\"SiteCollection\" />";

         DataTable dt = web.GetSiteData(query);
         DataView dv = new DataView(dt);

         // Set up the field bindings.
         BoundField boundField = new BoundField();
         boundField.HeaderText = "Last Name";
         boundField.DataField = "Title";
         this.grid.Columns.Add(boundField);

         boundField = new BoundField();
         boundField.HeaderText = "First Name";
         boundField.DataField = "FirstName";
         this.grid.Columns.Add(boundField);

         this.grid.AutoGenerateColumns = false;
         this.grid.DataSource = dv;
         this.grid.DataBind();

         this.grid.AllowSorting = true;
         this.grid.HeaderStyle.Font.Bold = true;

         this.grid.RenderControl(writer);
      }
   }
}

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

請參閱

參照

SPSiteDataQuery members

Microsoft.SharePoint namespace