top of page
Writer's pictureVijai Anand Ramalingam

How to query across multiple lists in multiple Web sites in in SharePoint

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.

  • You can specify the scope of the query by "Webs" property.

  • You can specify the lists to participate in the query by "Lists" property.

  • You can specify the fields to return by "ViewFields" property.

  • You can specify the order and data selection by "Query" property.

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:

a. Open Visual Studio 2010.

b. Select Visual webpart template from the installed templates and click on Ok.

c. My entire solution looks like the following.











c. Add the following code snippet in the .ascx file.

  1. <style type="text/css">   

  2. .style1   

  3. {       

  4. width: 100%;   

  5. }

  6. </style>

  7. <table class="style1">

  8. <tr>

  9. <td>

  10. <asp:Label ID="lblEmpID" runat="server" Text="Enter the Emp ID:"></asp:Label> &nbsp;&nbsp;

  11. <asp:TextBox ID="txtEmpId" runat="server"></asp:TextBox> &nbsp;

  12. <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click" Text="Search" />

  13. <br />

  14. <br />

  15. </td>

  16. </tr>

  17. <tr>

  18. <td>

  19. <asp:GridView ID="gvResult" runat="server" AutoGenerateColumns="False" EnableModelValidation="True">

  20. <Columns>

  21. <asp:BoundField DataField="Title" HeaderText="Name" />

  22. </Columns>

  23. </asp:GridView>

  24. </td>

  25. </tr>

  26. </table>

d. Add the following code snippet in the .ascx.cs file.

  1. protected void Page_Load(object sender, EventArgs e)       

  2. {           

  3. gvResult.Visible = false;                 

  4. }


  1. protected void btnSearch_Click(object sender,EventArgs e)       

  2. {

  3. using (SPSite site = new SPSite("http://serverName:1111/sites/SPSiteDataQuery/"))           

  4. {

  5. using (SPWeb web = site.RootWeb)               

  6. {

  7. SPSiteDataQuery dataQuery = new SPSiteDataQuery();                   

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

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

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

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

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

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

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

  15. dataQuery.Query = where;

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

  17. DataView dv =new DataView(dt);                   

  18. gvResult.DataSource = dv;                   

  19. gvResult.DataBind();                   

  20. gvResult.Visible = true;               

  21. }            

  22. }       

  23. } 

e. Build the solution.

f. Deploy the solution.

g. Go to the SharePoint site and add the custom web part.

h. In the textbox enter the EmployeeID value and click on search.

i. The result looks like the following.



0 comments

Comments


bottom of page