How to connect SharePoint Filter WebParts with Custom webpart

This post explains how to enable your custom webpart to read selected values from SharePoint’s Out-of-the-box filter webparts such as Choice filter, List filter etc.

Real life scenario can be that there is a custom webpart that displays SharePoint list data (with much better look and feel than OOB ListView webpart) and want to filter the list items based on the filter value selected from any OOB filter webparts. For example, you may be having your custom webpart configured to display data from ‘Tasks’ list but it should display tasks filtered on task list’s ‘Status’ field. The status can be picked from a choice filter webpart. You can add a Choice filter webpart with 2 choices i.e. ‘In Progress’, ‘completed’. Based on the selected status from Choice filter, custom webpart should display corresponding list items. But to enable your custom webpart to pick selected choice from Choice filter webpart, you need to modify its code as shown below.

Basically, all the filter webparts use ‘ITransformableFilterValues’ interface. Consumer webpart just needs to override its ‘FilterWebPartsConsumer’ method to get the provider details.

public class MyConsumerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
private ITransformableFilterValues filterWebPartsProvider;

[Personalizable(PersonalizationScope.Shared)]
public string FilterFieldName
{  get;  set;  }

[Personalizable(PersonalizationScope.Shared)]
public string FilterFieldValue
{  get; set; }

//Use with OOB Filter Webparts
[ConnectionConsumer(“Filter Consumer using FilterWebparts provider”, “FilterWebPartsConsumer”)]
public void FilterWebPartsConsumer(ITransformableFilterValues Provider)
{
filterWebPartsProvider = Provider;
}

protected override void OnPreRender(EventArgs e)
{
EnsureChildControls();
this.FilterFieldName = null;
this.FilterFieldValue = null;

if (filterWebPartsProvider != null)
{
this.FilterFieldName = filterWebPartsProvider.ParameterName;
ReadOnlyCollection filterValues = filterWebPartsProvider.ParameterValues;
if (filterValues != null)
{
foreach (string filterValue in filterValues)
{
this.FilterFieldValue += filterValue + “;”;
}
}
}

}
}

Trackbacks

Leave a Comment

Your email address will not be published. Required fields are marked *