SPSiteDataQuery can be used to perform cross-site and cross-list queries. In order to use it, you must call SPWeb.GetSiteData method on the appropriate web and provide a SPSiteDataquery object.
Using SPSiteDataQuery.Webs property you can specify where the data will be retrieved from. This property can have one of the following values:
- "" (or string.Empty) – default value : will search only the SPWeb on which you run GetSiteData
- "<Webs Scope='SiteCollection' />" : will search on all webs in the site collection
- "<Webs Scope='Recursive' />" : will search the SPWeb on which you run GetSiteData and all subwebs
You can restrict the search using Lists and Query properties. In the following example we query all contacts lists in the site collection for items containing John in the Last Name field:
SPSiteDataQuery query = new SPSiteDataQuery();
query.Lists = "<Lists ServerTemplate='105' />";
query.ViewFields = "<FieldRef Name='Title' />";
query.Query = "<Where><Contains>" +
"<FieldRef Name='Title' /><Value Type='text'>John</Value>" +
"</Contains></Where>";
query.Webs = "<Webs Scope='SiteCollection' />";
DataTable dt = SPContext.Current.Web.GetSiteData(query);
gdView.DataSource = dt;
gdView.DataBind();
No comments:
Post a Comment