Friday 28 March 2014

Sharepoint SPQuery vs SPSiteDataQuery

Sharepoint SPQuery vs SPSiteDataQuery
The SPQuery class is used to build query strings to filter or retrieve the specific set of data programmatically from a SP List object.
Syntax: SPQuery query = new SPQuery();
query.Query = <Where><Eq><FieldRef Name= “colname”/><Value type=”text”>filter parameter</value></Eq></Where>;
SPSiteDataQuery is used for cross-site and cross-list searching. SPSiteDataQuery can be used to search in all lists of a particular list type or list base type located in either the complete site collection or a particular site and sub-sites.
SPSiteDataQuery query = new SPSiteDataQuery();
query .ViewFields = “<FieldRef Name=”Title”/>”;
query .Lists = “<Lists ServerTemplate=”101″/>”;
query .Webs = “<Webs Scope=”SiteCollection”/>”;
query .Query = “<Where>” +
“<Contains>” +
“<FieldRef Name=”Title”/>” +
“<Value Type=”Text”>Conditional Parameter </Value>” +
“</Contains>” +
“</Where>”;
SPSite site = new SPSite(http://SP2010);
SPWeb web = site.OpenWeb();
DataTable dt = web.GetSiteData(query);
dt.Rows[0]["Title"]);
Points To Remember
Eq = Equal.
NEq= not equal conditions.
FieldRef Keeps the column name on which we want to apply the query.
Value Keeps the parameter for the query column.
We can use / before column name with / tags to have the corresponding NULL condition checks.