SPSiteDataQuery to get data from multiple lists in site collection level in sharepoint 2013
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace SPListDataQuery.ListDataQuery
{
[ToolboxItemAttribute(false)]
public class ListDataQuery : WebPart
{
private GridView grid;
private GridView myGrid;
private TextBox txtbox;
protected override void CreateChildControls()
{
this.grid = new GridView();
this.Controls.Add(this.grid);
this.myGrid = new GridView();
this.Controls.Add(this.myGrid);
this.txtbox = new TextBox();
this.Controls.Add(this.txtbox);
}
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
using(SPSite spsite=new SPSite("http://c4968397007/"))
{
using (SPWeb oweb = spsite.OpenWeb())
{
SPSiteDataQuery query = new SPSiteDataQuery();
query.Lists = "<Lists ServerTemplate=\"105\" />";
query.ViewFields = "<FieldRef Name=\"Title\" />" +
"<FieldRef Name=\"FirstName\" Nullable=\"TRUE\" Type=\"Text\"/>" +
"<FieldRef Name=\"Company\" Nullable=\"TRUE\" Type=\"Text\"/>";
query.Webs = "<Webs Scope=\"SiteCollection\" />";
DataTable dt = oweb.GetSiteData(query);
DataView dv = new DataView(dt);
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);
boundField = new BoundField();
boundField.HeaderText = "Company";
boundField.DataField = "Company";
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);
}
}
}
}
}
using Microsoft.SharePoint.WebControls;
using System;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace SPListDataQuery.ListDataQuery
{
[ToolboxItemAttribute(false)]
public class ListDataQuery : WebPart
{
private GridView grid;
private GridView myGrid;
private TextBox txtbox;
protected override void CreateChildControls()
{
this.grid = new GridView();
this.Controls.Add(this.grid);
this.myGrid = new GridView();
this.Controls.Add(this.myGrid);
this.txtbox = new TextBox();
this.Controls.Add(this.txtbox);
}
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
using(SPSite spsite=new SPSite("http://c4968397007/"))
{
using (SPWeb oweb = spsite.OpenWeb())
{
SPSiteDataQuery query = new SPSiteDataQuery();
query.Lists = "<Lists ServerTemplate=\"105\" />";
query.ViewFields = "<FieldRef Name=\"Title\" />" +
"<FieldRef Name=\"FirstName\" Nullable=\"TRUE\" Type=\"Text\"/>" +
"<FieldRef Name=\"Company\" Nullable=\"TRUE\" Type=\"Text\"/>";
query.Webs = "<Webs Scope=\"SiteCollection\" />";
DataTable dt = oweb.GetSiteData(query);
DataView dv = new DataView(dt);
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);
boundField = new BoundField();
boundField.HeaderText = "Company";
boundField.DataField = "Company";
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);
}
}
}
}
}
Comments