Tuesday, 11 February 2014

Sharepoint SPQuery vs SPSiteDataQuery

Query SharePoint List - SPQuery vs SPSiteDataQuery vs CrossListQueryInfo


There are three different classes you can use to query SharePoint list:-

1.       SPQuery

Used for query a data from a specific list and also used for join two lists.

Use full Links:-




2.       SPSiteDataQuery

Used for cross site query. Query a data within site collection or specific web.


 

3.       CrossListQueryInfo(The best one)

The CrossListQueryInfo object uses the CrossListQueryInfo object to get the cached results or, if there are no cached results available, it performs a cross-list query to the database and then caches the results for future use. Audience targeting is then applied to the result set, depending on the setting specified in the CrossListQueryInfo object. You can use the CbqQueryCacheobject to obtain a CrossListQueryInfo object for a specific Content by Query Web Part.


 
 
 
4.    KeywordQuery Class
 
            In order to develop custom search web parts or applications that support ‘search-by-keyword’ scenario, SharePoint Query object model exposesKeywordQuery Class.
 
 


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://Mywebapplication);
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.

No comments:

Post a Comment