Collection Of using SPSiteDataQuery in whole site

To set up a query across a whole site collection you can use the SPSiteDataQuery object.

The following method fetches all .doc files from all doclibs of the site collection and prints out a list of urls to those items.

public void TestSiteDataQuery()
using (SPSite site = new SPSite("http://localhost"))
using (SPWeb web = site.OpenWeb("/"))
SPSiteDataQuery query = new SPSiteDataQuery();

// Search in doclibs only
query.Lists = "<Lists BaseType='1' />";

// Only .doc files
query.Query =
<FieldRef Name='DocIcon' />
<Value Type='Computed'>doc</Value>

// Select only needed columns: file reference
query.ViewFields = "<FieldRef Name='FileRef' />";

// Search in all webs of the site collection
query.Webs = "<Webs Scope='SiteCollection' />";

// Perform the query
DataTable table = web.GetSiteData(query);

// Generate an absolute url for each document
foreach (DataRow row in table.Rows)
string relativeUrl =
row["FileRef"].ToString().IndexOf("#") + 1);
string fullUrl = site.MakeFullUrl(relativeUrl);

// Write urls to console

If you have better solution, just tell me !