SPSiteDataQuery 类

表示可以跨多个网站位于同一网站集中的多个列表执行的查询。

继承层次结构

System.Object
  Microsoft.SharePoint.SPSiteDataQuery

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

语法

声明
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属性的值。此外的模拟运算表包含一个名为ListId,用于标识包含每个项目的列表,列和名为ID,它标识每个项目的列的名为WebId,其中包含每个项目的网站列。

示例

下面的示例是查询创建的列表模板无处不在网站集中,检索每个联系人,并控制GridView中的信息的显示名和姓的联系人的所有列表 Web 部件。

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

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPSiteDataQuery 成员

Microsoft.SharePoint 命名空间