Webs property for SPSiteDataQuery in SharePoint 2010


In this article we will be seeing about the Web properties in SPSiteDataQuery in Sharepoint 2010.

The Webs property specifies which Web sites to include in the query. By default, the query considers only the Web site from which the GetSiteData method was invoked.

Scope attribute include Recursive and SiteCollection.

<Webs Scope="Recursive" />

<Webs Scope="SiteCollection" />

When the Scope attribute is set to Recursive, the query considers the current Web site and all subsites of the current Web site.

When the Scope attribute is set to SiteCollection, the query considers all Web sites that are in the same site collection as the current Web site.

I have a site collection SPSiteDataQuery which contains two subsites (Subsite1 and Subsite2).

SPSiteDataQuery1.gif
SPSiteDataQuery2.gif 

I have created a custom list definition with TemplateType="10000". Using the Custom List Definition I will be creating lists).

Custom List Definition:

SPSiteDataQuery3.gif


SPSiteDataQuery: Custom List

SPSiteDataQuery4.gif


Subsite1: Custom List1
 

SPSiteDataQuery5.gif

In the Subsite1 I have created a web part "SPSiteDataQuery WP" and I have created one subsite called "Test".

SPSiteDataQuery6.gif

In the "Test" Site I have created one list based on "Custom List Definition" called "CustomList".

SPSiteDataQuery7.gif


Using Scope = "Recursive":

When the Scope attribute is set to Recursive, the query considers the current Web site and all subsites of the current Web site.

Code Snippet:

SPSiteDataQuery dataQuery = new SPSiteDataQuery();
dataQuery.Webs = "<Webs Scope=\"Recursive\">";
dataQuery.Lists = "<Lists ServerTemplate=\"10000\" />";
dataQuery.ViewFields = "<FieldRef Name=\"Title\" />";
string where = "<Where><Eq>";
where += "<FieldRef Name=\"EmployeeID\"/>";
where += "<Value Type='Integer'>" + txtEmpId.Text + "</Value>";
where += "</Eq></Where>";

dataQuery.Query = where;
DataTable dt = web.GetSiteData(dataQuery);
DataView dv = new DataView(dt);
gvResult.DataSource = dv;
gvResult.DataBind();
gvResult.Visible = true;

SPSiteDataQuery8.gif

No result found when trying to search the data from SPSiteDataQuery site. Only values are retrieved from the current website and all subsites
of the current Web site.

SPSiteDataQuery9.gif

Using Scope="SiteCollection":

When the Scope attribute is set to SiteCollection, the query considers all Web sites that are in the same site collection as the current Web site.

Code Snippet:


SPSiteDataQuery dataQuery = new SPSiteDataQuery();
dataQuery.Webs = "<Webs Scope=\"SiteCollection\">";
dataQuery.Lists = "<Lists ServerTemplate=\"10000\" />";
dataQuery.ViewFields = "<FieldRef Name=\"Title\" />";
string where = "<Where><Eq>";
where += "<FieldRef Name=\"EmployeeID\"/>";
where += "<Value Type='Integer'>" + txtEmpId.Text + "</Value>";
where += "</Eq></Where>";

dataQuery.Query = where;
DataTable dt = web.GetSiteData(dataQuery);
DataView dv = new DataView(dt);
gvResult.DataSource = dv;
gvResult.DataBind();
gvResult.Visible = true;

SPSiteDataQuery10.gif

Refer also http://www.c-sharpcorner.com/UploadFile/anavijai/6336/ for SPSiteDataQuery.