I will take an example of searching files added in last 7 days in 'document library.
using (SPSite site = new Site("<site_url">)
{
using (SPWeb web = site.AllWebs["/"]) //Best practice to use SPWeb in using.
{
DataTable dt;
SPSiteDataQuery Query = new SPSiteDataQuery(); //Instantiate SPSiteDataQuery
//Date value in CAML query is accepted in a specific format. Below is the format accepted.
string str7DaysBackDateTime = (DateTime.Now.Add(new TimeSpan(-7, 0, 0, 0, 0))).ToString("yyyy-MM-ddThh:mm:ssZ");
string strQuery = String.Format("<Where><Gt><FieldRef Name=\"Modified\" />"
+"<Value Type=\"DateTime\">{0}</Value></Gt></Where>"
+"<OrderBy><FieldRef Ascending=\"FALSE\" Name=\"Modified\"/></OrderBy>"
, str7DaysBackDateTime);
Query.Query = strQuery; //Assign CAML query(without <Query> tag.
Query.RowLimit = 15; //Max Number of rows you want in the result.
StringBuilder sb = new StringBuilder();
sb.Append("<Lists>");
foreach (SPList list in web.Lists)
{
if (list.BaseType == SPBaseType.DocumentLibrary)
{
sb.Append("<List ID=\"" + list.ID.ToString() + "\"/>");
}
}
sb.Append("</Lists>");
Query.Lists = sb.ToString(); //The lists on which you want your query to be run.
dt = web.GetSiteData(Query);
}
}
I guess the above code is selft explanatory and is having enough comments also to explain the things.
akhilesh tiwari