I have created a search webpart and implemented on a site. Webpart contains 5 fields and as of now data can be searched from a single list. I have used caml query. Can anyone provide me caml query to fetch and search data from 4 different lists in the same site. Or do I need to follow some other procedure instead of caml query. Thanks

1 Answer 1


You can use the site data query to make it easier, its very similar to spquery. You can give the lists by using the Lists tag with guids of the list. Example:

 using (SPSite site = new SPSite("url"))
                using (SPWeb web = site.OpenWeb())

               SPSiteDataQuery query = new SPSiteDataQuery();

               // Query all Web sites in this site collection. (optional)
               query.Webs = "<Webs Scope=\"SiteCollection\">";

               //Ask for all lists created from the contacts template. (use list guids here)
               query.Lists = "<Lists ServerTemplate=\"105\" />";

               // Get the Title (Last Name) and FirstName fields.
               query.ViewFields = "<FieldRef Name=\"Title\" />"; 
               query.ViewFields += "<FieldRef Name=\"FirstName\" Nullable=\"TRUE\"/>";
               DataTable results = web.GetSiteData(query);

here is some reference to help you:

using Lists instead of using the template :

query.Lists ="<Lists>
      <List ID='7A9FDBE6-0841-430a-8D9A-53355801B5D5' />
      <List ID='3D18F506-FCA1-451e-B645-2D720DC84FD8' />


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.