Filtering SharePoint List data dynamically in Webpart
Add below Namespaces into the Standard WebPart:
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Xml; namespace DynamicallyFilterationList.ListFilterationDynamically
{
[ToolboxItemAttribute(false)]
public class ListFilterationDynamically : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox tbA;
TextBox tbB;
Button filterButton;
UpdatePanel mainUpdatePanel;
ListViewWebPart lvwp;
SPList list; protected override void CreateChildControls()
{ mainUpdatePanel = new UpdatePanel();
mainUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional; tbA = new TextBox();
tbB = new TextBox(); list = SPContext.Current.Web.GetList(SPUrlUtility.CombineUrl(SPContext.Current.Web.Url, "Lists/FilterList"));
lvwp = new ListViewWebPart();
lvwp.ListName = list.ID.ToString("B").ToUpper();
lvwp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
lvwp.ChromeType = PartChromeType.None; filterButton = new Button();
filterButton.Text = "Filter";
filterButton.Click += new EventHandler(filterButton_Click); Controls.Add(new LiteralControl(" Nom : "));
Controls.Add(tbA);
Controls.Add(new LiteralControl(" Prenom : "));
Controls.Add(tbB);
mainUpdatePanel.ContentTemplateContainer.Controls.Add(filterButton);
mainUpdatePanel.ContentTemplateContainer.Controls.Add(lvwp);
this.Controls.Add(mainUpdatePanel);
} private void filterButton_Click(object sender, EventArgs e)
{
string query = string.Empty;
string tempAdding = string.Empty;
if (!string.IsNullOrEmpty(tbA.Text))
{
query = "<Contains><FieldRef Name='LinkTitle' /><Value Type='Text'>" + tbA.Text + "</Value></Contains>";
}
if (!string.IsNullOrEmpty(tbB.Text))
{
tempAdding = "<Contains><FieldRef Name='Prenom' /><Value Type='Text'>" + tbB.Text + "</Value></Contains>";
query = (query.Length > 0 ? "<And>" + query + tempAdding + "</And>" : tempAdding);
} XmlDocument doc = new XmlDocument();
doc.LoadXml(lvwp.ListViewXml);
XmlNode queryNode = doc.SelectSingleNode("//Query");
XmlNode whereNode = queryNode.SelectSingleNode("Where"); if (whereNode != null) queryNode.RemoveChild(whereNode);
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "Where", String.Empty);
newNode.InnerXml = query.ToString();
queryNode.AppendChild(newNode);
lvwp.ListViewXml = doc.OuterXml; }
}
}
Ref: http://christopherclementen.wordpress.com/2012/04/02/filter-a-list-dynamically/ OutPut Screen:
Add below Namespaces into the Standard WebPart:
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Xml; namespace DynamicallyFilterationList.ListFilterationDynamically
{
[ToolboxItemAttribute(false)]
public class ListFilterationDynamically : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox tbA;
TextBox tbB;
Button filterButton;
UpdatePanel mainUpdatePanel;
ListViewWebPart lvwp;
SPList list; protected override void CreateChildControls()
{ mainUpdatePanel = new UpdatePanel();
mainUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional; tbA = new TextBox();
tbB = new TextBox(); list = SPContext.Current.Web.GetList(SPUrlUtility.CombineUrl(SPContext.Current.Web.Url, "Lists/FilterList"));
lvwp = new ListViewWebPart();
lvwp.ListName = list.ID.ToString("B").ToUpper();
lvwp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
lvwp.ChromeType = PartChromeType.None; filterButton = new Button();
filterButton.Text = "Filter";
filterButton.Click += new EventHandler(filterButton_Click); Controls.Add(new LiteralControl(" Nom : "));
Controls.Add(tbA);
Controls.Add(new LiteralControl(" Prenom : "));
Controls.Add(tbB);
mainUpdatePanel.ContentTemplateContainer.Controls.Add(filterButton);
mainUpdatePanel.ContentTemplateContainer.Controls.Add(lvwp);
this.Controls.Add(mainUpdatePanel);
} private void filterButton_Click(object sender, EventArgs e)
{
string query = string.Empty;
string tempAdding = string.Empty;
if (!string.IsNullOrEmpty(tbA.Text))
{
query = "<Contains><FieldRef Name='LinkTitle' /><Value Type='Text'>" + tbA.Text + "</Value></Contains>";
}
if (!string.IsNullOrEmpty(tbB.Text))
{
tempAdding = "<Contains><FieldRef Name='Prenom' /><Value Type='Text'>" + tbB.Text + "</Value></Contains>";
query = (query.Length > 0 ? "<And>" + query + tempAdding + "</And>" : tempAdding);
} XmlDocument doc = new XmlDocument();
doc.LoadXml(lvwp.ListViewXml);
XmlNode queryNode = doc.SelectSingleNode("//Query");
XmlNode whereNode = queryNode.SelectSingleNode("Where"); if (whereNode != null) queryNode.RemoveChild(whereNode);
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "Where", String.Empty);
newNode.InnerXml = query.ToString();
queryNode.AppendChild(newNode);
lvwp.ListViewXml = doc.OuterXml; }
}
}
Ref: http://christopherclementen.wordpress.com/2012/04/02/filter-a-list-dynamically/ OutPut Screen:
No comments:
Post a Comment