//Provider WebPart
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.Collections.Generic;
using System.Collections.ObjectModel;
namespace EventSearchWebPart
{
[Guid("0736764b-dc5c-404a-9c7b-188659cf2719")]
public class EventSearchWebPart : System.Web.UI.WebControls.WebParts.WebPart, ITransformableFilterValues
{
public EventSearchWebPart()
{
}
[ConnectionProvider("Department OR Location", "UniqueIDForRegionConnection", AllowsMultipleConnections = true)]
public ITransformableFilterValues SetConnection()
{
return this;
}
private RadioButtonList _regions;
protected override void CreateChildControls()
{
base.CreateChildControls();
_regions = new RadioButtonList();
_regions.Items.Add(new ListItem("Department"));
_regions.Items.Add(new ListItem("Location"));
_regions.Items.Add(new ListItem("All Events"));
_regions.AutoPostBack = true;
this.Controls.Add(_regions);
}
#region ITransformableFilterValues Members
public bool AllowAllValue
{
get { return true; }
}
public bool AllowEmptyValue
{
get { return false; }
}
public bool AllowMultipleValues
{
get { return false; }
}
public string ParameterName
{
get { return "Department OR Location"; }
}
public System.Collections.ObjectModel.ReadOnlyCollection<string> ParameterValues
{
get {
EnsureChildControls();
List<string> regions = new List<string>();
for (int i = 0; i < _regions.Items.Count; i++)
{
if (_regions.Items[i].Selected)
{
regions.Add(_regions.Items[i].Value);
}
}
ReadOnlyCollection<string> result = new ReadOnlyCollection<string>(regions);
return result;
}
}
#endregion
}
}
//Consumer WebPart
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.Collections.Generic;
namespace EventSearchWebPart
{
[Guid("558abb6e-c442-4cda-aaeb-991d8bdc6884")]
public class EventSearchConsumerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
private List<IFilterValues> _filterProviders;
private List<IFilterValues> FilterProviders
{
get
{
return _filterProviders;
}
}
[ConnectionConsumer("filter", "UniqueIDForConsumer", AllowsMultipleConnections = true)]
public void SetFilter(IFilterValues filterValues)
{
if (filterValues != null)
{
EnsureChildControls();
List<ConsumerParameter> parameters = new List<ConsumerParameter>();
parameters.Add(new ConsumerParameter("Department OR Location", ConsumerParameterCapabilities.SupportsMultipleValues | ConsumerParameterCapabilities.SupportsAllValue));
parameters.Add(new ConsumerParameter("Status", ConsumerParameterCapabilities.SupportsMultipleValues | ConsumerParameterCapabilities.SupportsAllValue));
filterValues.SetConsumerParameters(new System.Collections.ObjectModel.ReadOnlyCollection<ConsumerParameter>(parameters));
this.FilterProviders.Add(filterValues);
}
}
public EventSearchConsumerWebPart()
{
_filterProviders = new List<IFilterValues>();
}
protected override void OnPreRender(EventArgs e)
{
string filname = "";
string filval = "";
foreach (IFilterValues filter in FilterProviders)
{
//writer.WriteLine(string.Format("Parameter: {0} <br>", filter.ParameterName));
if (filter.ParameterValues != null)
{
foreach (string value in filter.ParameterValues)
if (!string.IsNullOrEmpty(value))
{
//writer.WriteLine(string.Format(" value: {0} <br>", value));
if (filter.ParameterName == "Department OR Location")
filname = value;
else
{
if (filter.ParameterName == "Status")
filval = value;
}
}
}
}
SPWeb web = SPControl.GetContextWeb(this.Context);
web.AllowUnsafeUpdates = true;
SPView view = web.Lists["Events01"].Views["All Documents"];
string query = "";
if (filname == "All Events")
query = "<OrderBy><FieldRef Name='Title' /></OrderBy>";
else
if (filname != "" && filval != "")
{
query = "<Where><Eq><FieldRef Name='" + filname + "' /><Value Type='Text'>" + filval + "</Value></Eq></Where>";
}
else
query = "<OrderBy><FieldRef Name='Title' /></OrderBy>";
view.Query = query;
view.Update();
base.OnPreRender(e);
}
}
}
|