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).











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

Custom List Definition:













SPSiteDataQuery: Custom List


Subsite1: Custom List1


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


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



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:

  1. SPSiteDataQuery dataQuery = new SPSiteDataQuery();

  2. dataQuery.Webs = "<Webs Scope=\"Recursive\">";

  3. dataQuery.Lists = "<Lists ServerTemplate=\"10000\" />";

  4. dataQuery.ViewFields = "<FieldRef Name=\"Title\" />";

  5. string where = "<Where><Eq>";

  6. where += "<FieldRef Name=\"EmployeeID\"/>";

  7. where += "<Value Type='Integer'>" + txtEmpId.Text + "</Value>";

  8. where += "</Eq></Where>";

  9. dataQuery.Query = where;

  10. DataTable dt = web.GetSiteData(dataQuery);

  11. DataView dv = new DataView(dt);

  12. gvResult.DataSource = dv;

  13. gvResult.DataBind();

  14. gvResult.Visible = true;



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.



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:

  1. SPSiteDataQuery dataQuery = new SPSiteDataQuery();

  2. dataQuery.Webs = "<Webs Scope=\"SiteCollection\">";

  3. dataQuery.Lists = "<Lists ServerTemplate=\"10000\" />";

  4. dataQuery.ViewFields = "<FieldRef Name=\"Title\" />";

  5. string where = "<Where><Eq>";

  6. where += "<FieldRef Name=\"EmployeeID\"/>";

  7. where += "<Value Type='Integer'>" + txtEmpId.Text + "</Value>";

  8. where += "</Eq></Where>";

  9. dataQuery.Query = where;

  10. DataTable dt = web.GetSiteData(dataQuery);

  11. DataView dv = new DataView(dt);

  12. gvResult.DataSource = dv;

  13. gvResult.DataBind();

  14. gvResult.Visible = true;



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