Sunday, September 7, 2008

Cross-list queries with SPSiteDataQuery

You need to query cross-list items across multiple Web sites

'SPSiteDataQuery' is more efficent for such a situation.
'SPQuery' should be used to query a particular list

Sample Code:

SPWeb webSite = SPContext.Current.Web;
SPSiteDataQuery query = new SPSiteDataQuery();

query.Lists = "<Lists ServerTemplate=\"107\" />";
query.Query = "<Where><Eq><FieldRef Name=\"Status\"/>" +
"<Value Type=\"Text\">Completed</Value></Eq></Where>";

System.Data.DataTable items = webSite.GetSiteData(query);

foreach (System.Data.DataRow item in items)
Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");


Anonymous,  June 17, 2010 at 8:09 AM  

If I want to get join data from multiple lists in single site using CAML, then how it is possible?

Sandeep K Nahta June 17, 2010 at 9:48 AM  

you will need to add logic to go through multiple site collections and display it or load it in data table to be able to bind it to some control