In this article we will be seeing about SPSiteDataQuery class which is used to query across multiple lists in multiple Web sites in the same Web site collection I have created a site collection (SPSiteDataQuery) along with that I have created two sub sites (Subsite1 and Subsite2) within the same site collection. I have created a custom list definition with TemplateType="10000". Using the Custom List Definition I am creating "Custom List" in all the sites (SPSiteDataQuery, Subsite1 and Subsite2). Custom List Definition: SPSiteDataQuery: Custom List Subsite1: Custom List Subsite2: Custom List SPSiteDataQueryWP web part: I have created a visual web part which looks like the following. SPSiteDataQuery: SPSiteDataQuery class is used to query across multiple lists in multiple Web sites in the same Web site collection.
Here we will be seeing how to query the multiple list which has the TemplateType=10000 ("Custom list" that I have created using "Custom List Definition" which has the TemplateType=10000 from SPSiteDataQuery, Subsite1 and Subsite2 sites.) We will be creating a web part which contains Label, Textbox, Search Button and a Grid view. In the textbox we will be entering the EmployeeID and on Search button click we will be retrieving the result from any of the list that we are querying. Steps Involved:
</td> </tr> </table>
protected void btnSearch_Click(object sender, EventArgs e) { using (SPSite site = new SPSite("http://serverName:1111/sites/SPSiteDataQuery/")) { using (SPWeb web = site.RootWeb) { 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; }
} }
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: