Monday, June 2, 2008

Using SPSiteDataQuery class to execute cross site queries

Hi All,

SPSiteDataQuery Class can be used to query several lists and several document libraries in different sites or from same site in web application.

May times we require to query against lists and query against more than one document libraries of same site or even from different sites. At that point of time we can use this object.
As name implies it’s really a SiteDataQuery, query that can be performed at site levels.

Interesting feature is that SPQuery returns result as SPListItemCollection, SPSiteDataQuery returns result as DataTable which is very useful.

This example performs a query against all document libraries in a particular Web site and returns the file paths for the items whose ID is greater than 1.

SPWeb oWebsite = SPContext.Current.Web;
SPSiteDataQuery oQuery = new SPSiteDataQuery();
string strWhere = "<Where><Gt>" +
"<FieldRef Name=\"ID\" />" +
"<Value Type=\"Number\">1</Value>" +

string strOrderBy = "<OrderBy><FieldRef Name=\"FileRef\" /></OrderBy>";

oQuery.Query = strWhere + strOrderBy;

oQuery.Lists = "<Lists ServerTemplate=\"101\" />";
oQuery.ViewFields = "<FieldRef Name=\"Title\" />";

DataTable dtResults = oWebsite.GetSiteData(oQuery);

Make a point that by default limit of Lists that you can include in query is 1000 Lists. (Which is more than enough I think).

There is a good article available to read further about SPSiteQuery.
Click Here

For MSDN Link : Click Here

No comments:

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to