Represents a query that can be performed across multiple lists in multiple Web sites in the same Web site collection.
You can use an instance of this class to retrieve data from selected lists or from all lists in the current site collection. Specify the scope of the query by setting the Websproperty. Specify the lists to participate in the query by setting the Lists property and the fields to return by setting the ViewFields property. Control data selection and order by setting the Query property.
To execute the query, pass the SPSiteDataQuery object to the GetSiteData(SPSiteDataQuery) method, which returns a DataTable object containing rows of data that represent the result of the query. Each DataRow object in the DataTable.Rows collection represents a single item that satisfies the query. Each DataColumn object in theDataTable.Columns collection represents a field that is requested in the ViewFields property, and the column name equals the value of the Name attribute for that field. In addition, the data table contains a column named WebId, which identifies the Web site that contains each item, a column named ListId, which identifies the list that contains each item, and a column named ID, which identifies each item.
Importante:
The enumeration value for Contacts is 105.
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);
}
}
}
Una recomendacion final es no abusar de SPSiteDataQuery para realizar todo tipo de consultas,recuerda que SPSiteDataQuery realiza la consulta en toda una coleccion de sitio, si solamenterequieres buscar sobre un mismo nivel de sitio es mucho mas recomendable utiliza SPQuery.
Más información: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.aspx
http://geeks.ms/blogs/haarongonzalez/archive/2007/10/03/spsitedataquery-que-belleza.aspx
http://sadomovalex.blogspot.com/2011/02/problem-with-spsitedataquery-and.html
No hay comentarios:
Publicar un comentario