Showing posts with label Sharepoint SPQuery. Show all posts
Showing posts with label Sharepoint SPQuery. Show all posts

Wednesday, 23 July 2014

SharePoint SPQuery

public DataTable BindSPData(string txtSPNumber, 
string txtTitle, string strOriginator)
{
 
WindowsIdentity objWindowsIdentity = WindowsIdentity.GetCurrent();
WindowsImpersonationContext objWindowsImpersonationContext = null;
RevertToSelf();
SPQuery oquery = null;
SPSite siteCollection = null;
SPWeb topLevelSite = null;
SPListItemCollection myitems = null;
try
{
objWindowsImpersonationContext = WindowsIdentity.GetCurrent().Impersonate();
siteCollection = new SPSite(System.Configuration.
ConfigurationManager.AppSettings["SPSPSite"]);
 
topLevelSite = siteCollection.AllWebs[System.Configuration.
ConfigurationManager.AppSettings["SPTopLevelSite"]];
SPList list = topLevelSite.Lists[System.Configuration.
ConfigurationManager.AppSettings["SPListName"]];
topLevelSite.AllowUnsafeUpdates = true;
 
//Create an instance of SPQuery Object
oquery = new SPQuery();
 
if (((txtSPNumber != “”) & (txtTitle != “”)) || 
((txtTitle != “”) & (strOriginator != “”)) || ((strOriginator != “”) & 
(txtSPNumber != “”)))
{
oquery.Query = “”;
}
else
{
if ((txtSPNumber != “”) || (txtTitle != “”) || (strOriginator != “”))
{
oquery.Query = “”;
}
}
if (txtSPNumber != “”)
{
oquery.Query += “” + txtSPNumber + “”;
}
if (txtTitle != “”)
{
oquery.Query += “” + txtTitle + “”;
}
if (strOriginator != “”)
{
oquery.Query += “” + strOriginator + “”;
}
if (((txtSPNumber != “”) & (txtTitle != “”)) || ((txtTitle != “”) &
 (strOriginator != “”)) || ((strOriginator != “”) & (txtSPNumber != “”)))
{
oquery.Query += “”;
}
else
{
if ((txtSPNumber != “”) || (txtTitle != “”) || (strOriginator != “”))
{
oquery.Query += “”;
}
}
 
DataTable dtNewItems;
//Assign the query object to myitems List Collection
if ((oquery.Query != “”) & (oquery.Query != null))
{
myitems = list.GetItems(oquery);
dtNewItems = myitems.GetDataTable();
}
else
{
myitems = list.Items;
//Get the Datatable for ALL the list items
dtNewItems = myitems.GetDataTable();
}
return dtNewItems;
}
catch (Exception ex)
{
throw ex;
}
finally
{
objWindowsImpersonationContext.Undo();
topLevelSite.AllowUnsafeUpdates = false;
siteCollection.Dispose();
topLevelSite.Dispose();
}
}
—————————————–
SPQuery query = new SPQuery();
query.Query = “10?;
SPQuery query = new SPQuery();
query.Query = “10?;

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.