Here are U2U we love to use CAML queries for retrieving items out of lists and document libraries. The SharePoint product team has added a very useful new class in the WSS v3 object model called the SPSiteDataQuery class. It allows you to execute your CAML queries against all lists or document libraries across all of your sub sites or even all of the sites in your site collection. I just have submitted an article to the Advisor SharePoint magazine explaining this in detail.
Take for example the scenario where you want to have a Web Part displaying all of the documents added to all document libraries in all of your sites. Here is the code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Utilities;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace TodaysDocuments
{
public class TodaysDocuments: WebPart
{
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
SPSite site = SPControl.GetContextSite(this.Context);
SPWeb web = site.OpenWeb();
SPSiteDataQuery qry = new SPSiteDataQuery();
string date = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today);
qry.Query = "" +
"" +
"" + date +
"";
qry.Lists = "";
qry.ViewFields = "";
qry.Webs = "";
DataTable tbl = web.GetSiteData(qry);
writer.Write("Documents added today to the sites.
");
DataGrid grid = new DataGrid();
grid.DataSource = tbl;
grid.DataBind();
grid.RenderControl(writer);
}
}
}
I did not really care about the presentation but I am pretty sure you guys can take it form here. The beta 2 SDK is available for download if you want to study this further.
The result is the following: