SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

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
 


Posted 10-11-2007 10:19 PM by Akhilesh Tiwari

Comments

University Update-Windows Vista-Query Multiple Lists using SPSiteDataQuery class wrote University Update-Windows Vista-Query Multiple Lists using SPSiteDataQuery class
on 10-11-2007 5:48 PM

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

Links (10/11/2007) « Steve Pietrek’s SharePoint Stuff wrote Links (10/11/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff
on 10-11-2007 7:39 PM

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

Alexandro Silva wrote re: Query Multiple Lists using SPSiteDataQuery class
on 10-11-2007 10:44 PM

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!

Alexei Strots wrote re: Query Multiple Lists using SPSiteDataQuery class
on 11-26-2007 6:37 PM

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!

Ven wrote re: Query Multiple Lists using SPSiteDataQuery class
on 11-13-2008 10:41 AM

Akhilesh,

               How can we query multiple lists if the fields in the lists are different?

Patric wrote re: Query Multiple Lists using SPSiteDataQuery class
on 11-28-2008 4:29 AM

I want to do the same like Akhilesh. We have many task lists, but not always the same fields for the AssignedTo User.... Is there a "best practise" for this scenario?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.