Search This Blog

2009-04-30

SPSiteDataQuery

GHow we can use SPSiteDataQuery

SPSiteDataQuery object is used to perform the crosslist queries.As we have seen before that SPQuery is used to perform the query on a single list at a time, what if We want to run the same Query on multiple list on the websites in the site collection ? SPSiteDataQuery solves this purpose .It runs the query on multiple list and give all the results merged in a datatable.We Use

SPWeb.GetSiteData(SPSiteDataQueryObj) to get the final Datatable.

Properties :-
Lists
Gets or sets the inner XML that specifies which lists to include in the query.
Query
Gets or sets the inner XML that defines the query.
RowLimit
Gets or sets a limit for the number of items in the query that are returned per page.
ViewFields
Gets or sets the inner XML that describes the view fields used in the query.
Webs
Gets or sets the inner XML that specifies which Web sites to include in the query as specified by the Scope attribute on the Webs tag in the query. By default, the query considers the current Web site, that is, the Web site from which the GetSiteData method was invoked.

Note:-

Webs

Possible values of the Scope attribute include Recursive and SiteCollection. When the Scope attribute is set to Recursive, the query considers all Web sites that are descended from the current SPWeb object. For example: When the Scope attribute is set to SiteCollection, the query considers all Web sites that are in the same site collection as the current Web site.


Lists Attributes
Supported optional attributes for the Lists tag include the following:
· ServerTemplate -- Limits the query to lists of the specified server template. Example: <Lists ServerTemplate="104" />
· BaseType -- Limits the query to lists of the specified base type. Example: <Lists BaseType="1" />
The following table lists possible values for the default base types.


Value Description
0 Generic list
1 Document library
3 Discussion forum
4 Vote or Survey
5 Issues list

· Hidden -- Determines whether the query will include hidden lists. Example: <Lists Hidden = "TRUE />By default, the query will consider all non-hidden lists of base type 0.· MaxListLimit -- Limits the query to the total number of lists specified. If the query would exceed the limit, the query will instead fail and raise an SPException. By default, the limit is 1000. When set to 0, there is no limit to the number of lists that will be considered. Example: <Lists MaxListLimit="500" />

Lists Subelements
Possible subelements of the Lists tag include List and WithIndex.
· The List tag allows the query to include specific lists, instead of returning all lists of a particular type. The ID attribute identifies each list. Example:

<Lists> <List ID="7A9FDBE6-0841-430a-8D9A-53355801B5D5" /> <List ID="3D18F506-FCA1-451e-B645-2D720DC84FD8" /></Lists>

Code Snipet Example :-
SPWeb oWebsite = SPContext.Current.Web;
SPSiteDataQuery oQuery = new SPSiteDataQuery();
string strWhere = "<Where><Gt>" + "<FieldRef Name=\"ID\" />" + "<Value Type=\"Number\">1</Value>" + "</Gt></Where>";
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);

No comments: