in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

akhileshtiwari

Query Multiple Lists using SPSiteDataQuery class

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
 

Comments

 

University Update-Windows Vista-Query Multiple Lists using SPSiteDataQuery class said:

Pingback from  University Update-Windows Vista-Query Multiple Lists using SPSiteDataQuery class

October 11, 2007 5:48 PM
 

Links (10/11/2007) « Steve Pietrek’s SharePoint Stuff said:

Pingback from  Links (10/11/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

October 11, 2007 7:39 PM
 

Alexandro Silva said:

Hi Mate, nice tip!!

The SPSiteDataQuery class is really a very useful tool when we need to query some data accross lists in a single site or even over different sites in a site collection.

Unfortunately, it has some known limitations, such as the amount of lists which we can aggregate data from.

BTW, for a large range of scenarios, it is still the most suitable way for aggregating and querying data.

Just a quick tip: if you want to query all of the lists of the SPBaseType.DocumentLibrary, you can refer to its list definition code, instead of looping through the whole list collection and adding each of their IDs to the <Lists> node, such as:

Query.Lists = "<Lists BaseType='1'/>";

or, for lists from a specific template:

Query.Lists = "<Lists ServerTemplate='222'/>"

Cheers!

October 11, 2007 10:44 PM
 

Alexei Strots said:

Hello Akhilesh,

If you would read my previous emails you will find that line:

using (SPWeb web = SPContext.Current.Web)  

is wrong, because SPContext.Current is managed by SP and your code can potantially cause an error.

Have a good one!

November 26, 2007 6:37 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts