//Filter And Bind data to Grid
public void BindGridTEST()
{
using (SPSite site = new SPSite(ConfigurationManager.AppSettings["InternalSiteURL"]))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPQuery query = new SPQuery();
this.Status = "PRO";
if (!string.IsNullOrEmpty(Convert.ToString(txtCName.Text)))
this.CName = txtCName.Text.Trim();
if (!string.IsNullOrEmpty(Convert.ToString(txtAName.Text)))
this.AName = txtAName.Text.Trim();
if (!string.IsNullOrEmpty(Convert.ToString(txtCode.Text)))
this.Code = txtCode.Text.ToLower().Trim();
if (!string.IsNullOrEmpty(Convert.ToString(txtPDate.Text)))
this.PDate = txtPDate.Text.ToLower().Trim();
//Generate CAML Query Dynamically
query.Query = Convert.ToString(getCAMLSerachQuery());
// queryTrainingDetails.QueryThrottleMode = SPQueryThrottleOption.Override;
query.RowLimit = 1000;
query.IncludeMandatoryColumns = false;
query.ViewFields = string.Concat(@"<FieldRef Name='Name' />
<FieldRef Name='Date' />
<FieldRef Name='Code' />
<FieldRef Name='ID' />
<FieldRef Name='Status' />";
query.ViewFieldsOnly = true;
SPListItemCollection listitems;
listitems = web.Lists["ListName"].GetItems(query);
DataTable dt = null;
if (listitems != null && listitems.Count > 0)
{
int fieldCount = listitems.Fields.Count;
dt = listitems.GetDataTable();
lblNoRecordsMsg.Text = "";
gvGIPropDetails.Visible = true;
trchkSelectAll.Visible = true;
Cbx_Toggle.Checked = false;
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
else
{
lblNoRecordsMsg.Text = "No Record(s) Found.";
gvDetails.Visible = false;
trchkSelectAll.Visible = false;
}
}
}
}
#region Sorting Properties
private string _SortField = "ID";
private bool _SortDirection = false;
public string SortField
{
get { return _SortField; }
set { _SortField = value; }
}
public bool SortDirection
{
get { return _SortDirection; }
set { _SortDirection = value; }
}
#endregion Sorting Properties
#region Search Criteria Properties
private string _CName, _AName, _Insurer, _PropDate, _Status;
public string Status
{
get { return _Status; }
set { _Status = value; }
}
public string CName
{
get { return _CName; }
set { _CName = value; }
}
public string AdviserName
{
get { return _AName; }
set { _AName = value; }
}
public string Code
{
get { return _Code; }
set { _Code = value; }
}
public string PDate
{
get { return _PDate; }
set { _PDate = value; }
}
public string CAML_CName
{
get
{
return string.Format(@"<Contains><FieldRef Name='CName'/>" +
"<Value Type='Text'>{0}</Value></Contains>", this.CName);
}
}
public string CAML_AName
{
get
{
return string.Format(@"<Contains><FieldRef Name='AName'/>
<Value Type='Text'>{0}</Value></Contains>", this.AName);
}
}
public string CAML_Insurer
{
get
{
return string.Format(@"<Contains><FieldRef Name='Code'/>
<Value Type='Text'>{0}</Value></Contains>", this.Code);
}
}
public string CAML_PDate
{
get
{
return string.Format(@"<Eq><FieldRef Name='PDate'/>
<Value Type ='DateTime' IncludeTimeValue='FALSE'>{0}</Value></Eq>", String.Format("{0:s}", Convert.ToDateTime(this.PDate)));
}
}
public string CAML_Status
{
get
{
return string.Format(@"<Eq><FieldRef Name='Status'/>
<Value Type='Text'>{0}</Value></Eq>", "PRO");
}
}
//Build CAML Query using AND Operator
public string BuildCamlQuery
{
get
{
List<string> objCaml = new List<string>();
StringBuilder _caml = new StringBuilder();
if (!string.IsNullOrEmpty(this.ClientName))
objCaml.Add(this.CAML_CName);
if (!string.IsNullOrEmpty(this.Insurer))
objCaml.Add(this.CAML_Insurer);
if (!string.IsNullOrEmpty(this.AName))
objCaml.Add(this.CAML_AName);
if (!string.IsNullOrEmpty(Convert.ToString(this.PDate)))
objCaml.Add(this.CAML_PDate);
if (!string.IsNullOrEmpty(Convert.ToString(this.Status)))
objCaml.Add(this.CAML_Status);
for (int i = 1; i < objCaml.Count; i++)
{
_caml.Append("<And>");
}
//Now create a string out of the CMAL snippets in the list.
for (int i = 0; i < objCaml.Count; i++)
{
string snippet = objCaml[i];
_caml.AppendFormat(snippet);
if (i == 1)
{
_caml.Append("</And>");
}
else if (i > 1)
{
_caml.Append("</And>");
}
}
string value = string.Empty;
if (_caml.ToString().Trim().Length > 0)
value = string.Format(@"<Where>{0}</Where>", _caml.ToString().Trim());
//Return the final CAML query
return value;
}
}
//Apend Sort Option to CAML Query
public StringBuilder getCAMLSerachQuery()
{
StringBuilder camlQuery = new StringBuilder();
if (this.BuildCamlQuery != null && this.BuildCamlQuery != string.Empty)
{
camlQuery.Append(this.BuildCamlQuery);
}
camlQuery.AppendFormat(@"<OrderBy><FieldRef Name='{0}' Ascending='{1}' /></OrderBy>", this.SortField, this.SortDirection);
return camlQuery;
}
#endregion Search Criteria Properties
public void BindGridTEST()
{
using (SPSite site = new SPSite(ConfigurationManager.AppSettings["InternalSiteURL"]))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPQuery query = new SPQuery();
this.Status = "PRO";
if (!string.IsNullOrEmpty(Convert.ToString(txtCName.Text)))
this.CName = txtCName.Text.Trim();
if (!string.IsNullOrEmpty(Convert.ToString(txtAName.Text)))
this.AName = txtAName.Text.Trim();
if (!string.IsNullOrEmpty(Convert.ToString(txtCode.Text)))
this.Code = txtCode.Text.ToLower().Trim();
if (!string.IsNullOrEmpty(Convert.ToString(txtPDate.Text)))
this.PDate = txtPDate.Text.ToLower().Trim();
//Generate CAML Query Dynamically
query.Query = Convert.ToString(getCAMLSerachQuery());
// queryTrainingDetails.QueryThrottleMode = SPQueryThrottleOption.Override;
query.RowLimit = 1000;
query.IncludeMandatoryColumns = false;
query.ViewFields = string.Concat(@"<FieldRef Name='Name' />
<FieldRef Name='Date' />
<FieldRef Name='Code' />
<FieldRef Name='ID' />
<FieldRef Name='Status' />";
query.ViewFieldsOnly = true;
SPListItemCollection listitems;
listitems = web.Lists["ListName"].GetItems(query);
DataTable dt = null;
if (listitems != null && listitems.Count > 0)
{
int fieldCount = listitems.Fields.Count;
dt = listitems.GetDataTable();
lblNoRecordsMsg.Text = "";
gvGIPropDetails.Visible = true;
trchkSelectAll.Visible = true;
Cbx_Toggle.Checked = false;
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
else
{
lblNoRecordsMsg.Text = "No Record(s) Found.";
gvDetails.Visible = false;
trchkSelectAll.Visible = false;
}
}
}
}
#region Sorting Properties
private string _SortField = "ID";
private bool _SortDirection = false;
public string SortField
{
get { return _SortField; }
set { _SortField = value; }
}
public bool SortDirection
{
get { return _SortDirection; }
set { _SortDirection = value; }
}
#endregion Sorting Properties
#region Search Criteria Properties
private string _CName, _AName, _Insurer, _PropDate, _Status;
public string Status
{
get { return _Status; }
set { _Status = value; }
}
public string CName
{
get { return _CName; }
set { _CName = value; }
}
public string AdviserName
{
get { return _AName; }
set { _AName = value; }
}
public string Code
{
get { return _Code; }
set { _Code = value; }
}
public string PDate
{
get { return _PDate; }
set { _PDate = value; }
}
public string CAML_CName
{
get
{
return string.Format(@"<Contains><FieldRef Name='CName'/>" +
"<Value Type='Text'>{0}</Value></Contains>", this.CName);
}
}
public string CAML_AName
{
get
{
return string.Format(@"<Contains><FieldRef Name='AName'/>
<Value Type='Text'>{0}</Value></Contains>", this.AName);
}
}
public string CAML_Insurer
{
get
{
return string.Format(@"<Contains><FieldRef Name='Code'/>
<Value Type='Text'>{0}</Value></Contains>", this.Code);
}
}
public string CAML_PDate
{
get
{
return string.Format(@"<Eq><FieldRef Name='PDate'/>
<Value Type ='DateTime' IncludeTimeValue='FALSE'>{0}</Value></Eq>", String.Format("{0:s}", Convert.ToDateTime(this.PDate)));
}
}
public string CAML_Status
{
get
{
return string.Format(@"<Eq><FieldRef Name='Status'/>
<Value Type='Text'>{0}</Value></Eq>", "PRO");
}
}
//Build CAML Query using AND Operator
public string BuildCamlQuery
{
get
{
List<string> objCaml = new List<string>();
StringBuilder _caml = new StringBuilder();
if (!string.IsNullOrEmpty(this.ClientName))
objCaml.Add(this.CAML_CName);
if (!string.IsNullOrEmpty(this.Insurer))
objCaml.Add(this.CAML_Insurer);
if (!string.IsNullOrEmpty(this.AName))
objCaml.Add(this.CAML_AName);
if (!string.IsNullOrEmpty(Convert.ToString(this.PDate)))
objCaml.Add(this.CAML_PDate);
if (!string.IsNullOrEmpty(Convert.ToString(this.Status)))
objCaml.Add(this.CAML_Status);
for (int i = 1; i < objCaml.Count; i++)
{
_caml.Append("<And>");
}
//Now create a string out of the CMAL snippets in the list.
for (int i = 0; i < objCaml.Count; i++)
{
string snippet = objCaml[i];
_caml.AppendFormat(snippet);
if (i == 1)
{
_caml.Append("</And>");
}
else if (i > 1)
{
_caml.Append("</And>");
}
}
string value = string.Empty;
if (_caml.ToString().Trim().Length > 0)
value = string.Format(@"<Where>{0}</Where>", _caml.ToString().Trim());
//Return the final CAML query
return value;
}
}
//Apend Sort Option to CAML Query
public StringBuilder getCAMLSerachQuery()
{
StringBuilder camlQuery = new StringBuilder();
if (this.BuildCamlQuery != null && this.BuildCamlQuery != string.Empty)
{
camlQuery.Append(this.BuildCamlQuery);
}
camlQuery.AppendFormat(@"<OrderBy><FieldRef Name='{0}' Ascending='{1}' /></OrderBy>", this.SortField, this.SortDirection);
return camlQuery;
}
#endregion Search Criteria Properties
No comments:
Post a Comment