Monday 30 June 2014

Sharepoint List Multiline text Filter web part

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.ComponentModel;
using System.Drawing;
namespace WebPart1
{
[Guid("bc7e8f82-29ef-447d-9276-c52cb98addd1")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtSearch;
Label lblmsg;
Button btnFilter;
public WebPart1()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
lblmsg = new Label();
txtSearch = new TextBox();
btnFilter = new Button();
btnFilter.Text = "Filter";
btnFilter.Click += new EventHandler(btnFilterClick);
this.Controls.Add(txtSearch);
//this.Controls.Add(new LiteralControl("
"));
this.Controls.Add(btnFilter);
this.Controls.Add(new LiteralControl("
"));
this.Controls.Add(lblmsg);
//load all the data
string oquery = "";
try
{
SPContext.Current.Web.AllowUnsafeUpdates = true;
if (txtSearch.Text.Trim() != "")
{
oquery = "";
oquery += "" + txtSearch.Text.Trim() + "";
oquery += "";
}
Microsoft.SharePoint.SPViewCollection viewColl = SPContext.Current.Web.Lists["TestFilter"].Views;
for (int i = 0; i < viewColl.Count; i++)
{
Microsoft.SharePoint.SPView view = viewColl[i];
view.Query = oquery;
view.Update();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
SPContext.Current.Web.AllowUnsafeUpdates = false;
txtSearch.Text = "";
}
}
protected void btnFilterClick(object sender, EventArgs e)
{
if (txtSearch.Text.Trim() == "")
{
lblmsg.Text = "You must specify a value for this required field";
string oquery = "";
try
{
SPContext.Current.Web.AllowUnsafeUpdates = true;
if (txtSearch.Text.Trim() != "")
{
oquery = "";
oquery += "" + txtSearch.Text.Trim() + "";
oquery += "";
}
Microsoft.SharePoint.SPViewCollection viewColl = SPContext.Current.Web.Lists["TestFilter"].Views;
for (int i = 0; i < viewColl.Count; i++)
{
Microsoft.SharePoint.SPView view = viewColl[i];
view.Query = oquery;
view.Update();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
SPContext.Current.Web.AllowUnsafeUpdates = false;
txtSearch.Text = "";
}
}
else
{
string oquery = "";
try
{
SPContext.Current.Web.AllowUnsafeUpdates = true;
if (txtSearch.Text.Trim() != "")
{
oquery = "";
oquery += "" + txtSearch.Text.Trim() + "";
oquery += "";
}
Microsoft.SharePoint.SPViewCollection viewColl = SPContext.Current.Web.Lists["TestFilter"].Views;
for (int i = 0; i < viewColl.Count; i++)
{
Microsoft.SharePoint.SPView view = viewColl[i];
view.Query = oquery;
view.Update();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
SPContext.Current.Web.AllowUnsafeUpdates = false;
txtSearch.Text = "";
}
}
}
}
}