How to: Configure the SharePoint List Filter Web Part programmatically

Applies to SharePoint List Filter Web Part.

In this post I will describe how to configure the SharePoint List Filter Web Part programmatically.

Below I have provided a sample method that adds a List Filter Web Part to the top of a view page.

The Filter Web Part is configured as follows:

  • all view fields are displayed in the filter details
  • SharePoint Search is used for keyword search (SearchFieldsString is empty)
/// 
/// Adds a filter web part to the view page
/// 
/// The view
private void AddFilterWebPart(SPView v)
{
    //Open web part manager of the view page
    SPLimitedWebPartManager wpm = v.ParentList.ParentWeb.GetLimitedWebPartManager(v.ServerRelativeUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
    //Get list view web part
    var lvwp = wpm.WebParts.Cast().Where(wp => wp is XsltListViewWebPart).Select(wp => wp as XsltListViewWebPart).FirstOrDefault();
    if (lvwp != null)
    {
        //Create and configure FilterWebPart
        sapiens.at.SharePoint.FilterWebPart.FilterWebPart wp = new sapiens.at.SharePoint.FilterWebPart.FilterWebPart();
        //The StorageKey of the List View Web Part as string
        //wp.ConnectedWebPartId = "38a26914-07a7-4481-ad34-f092d920cedc";
        wp.ConnectedWebPartId = lvwp.StorageKey.ToString();
        //The Fields you want to display in the filter details, separated by ;#
        //Format: InternalName;PossibleValues;UseWildCard;AllowMultiSelection (see configuration toolpart)
        //wp.FilterFields = "Title;false;false;true;#Created;false;false;false;#Author;false;false;true";
        wp.FilterFields = string.Join(";#", lvwp.View.ViewFields.Cast().Select(f => f + ";false;false;true"));
        //Use Search
        wp.UseServerSearch = true;
        //Fields to use for keyword search (Separated by ;# - empty if search is used)
        wp.SearchFieldsString = "";
        //row limit for the search
        wp.ServerSearchRowLimit = 500;
        //start with keyword search
        wp.StartWithKeywordSearch = false;
        //display export to excell button
        wp.DisplayExportButton = true;
        //display filter details on default
        wp.DisplayFilterDetailsOnDefault = true;
        //display filter manu for predefined filters
        wp.DisplayFilterMenu = true;
        //display print button
        wp.DisplayPrintButton = true;
        //display search scope (if more than one view is configured)
        wp.DisplaySearchScope = false;
        //max number of items for multi choice filters
        wp.MaxNumberOfItems = 500;
        //display performance notification for administrators
        wp.DisplayPerformanceNotification = false;
        //number of columns in the filter details
        wp.NumberOfColumns = 3;
        //Expand and collapse list views (if title row of the web part is displayed)
        wp.AllowExpandCollapse = true;
        //Display a button to apply the filters instead of filtering immediately.
        wp.DisplayFilterButton = false;
        //Display total number of items and items in the result set.
        wp.DisplayItemCount = false;
        //Applies only to managed metadata fields with "All possible values" selected. In this case the entire managed metadata tree will be displayed.
        wp.IncludeDescendants = false;
        //Only show results if a filter is applied (improves performance on initial load)
        wp.HideViewByDefault = false;
        //Distinct Lookup Values
        wp.DistinctLookupValues = false;
        //IsConfigured flag
        wp.IsConfigured = true;
        //add the web part to the page
        wpm.AddWebPart(wp, "Main", 0);
    }
}
/// 
/// Adds a filter web part to the view page
/// 
/// The view
private void AddFilterWebPart(SPView v)
{
    //Open web part manager of the view page
    SPLimitedWebPartManager wpm = v.ParentList.ParentWeb.GetLimitedWebPartManager(v.ServerRelativeUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
    //Get list view web part
    var lvwp = wpm.WebParts.Cast().Where(wp => wp is XsltListViewWebPart).Select(wp => wp as XsltListViewWebPart).FirstOrDefault();
    if (lvwp != null)
    {
        //Create and configure FilterWebPart
        sapiens.at.SharePoint.FilterWebPart.FilterWebPart wp = new sapiens.at.SharePoint.FilterWebPart.FilterWebPart();
        //The StorageKey of the List View Web Part as string
        //wp.ConnectedWebPartId = "38a26914-07a7-4481-ad34-f092d920cedc";
        wp.ConnectedWebPartId = lvwp.StorageKey.ToString();
        //The Fields you want to display in the filter details, separated by ;#
        //Format: InternalName;PossibleValues;UseWildCard;AllowMultiSelection (see configuration toolpart)
        //wp.FilterFields = "Title;false;false;true;#Created;false;false;false;#Author;false;false;true";
        wp.FilterFields = string.Join(";#", lvwp.View.ViewFields.Cast().Select(f => f + ";false;false;true"));
        //Use Search
        wp.UseServerSearch = true;
        //Fields to use for keyword search (Separated by ;# - empty if search is used)
        wp.SearchFieldsString = "";
        //row limit for the search
        wp.ServerSearchRowLimit = 500;
        //start with keyword search
        wp.StartWithKeywordSearch = false;
        //display export to excell button
        wp.DisplayExportButton = true;
        //display filter details on default
        wp.DisplayFilterDetailsOnDefault = true;
        //display filter manu for predefined filters
        wp.DisplayFilterMenu = true;
        //display print button
        wp.DisplayPrintButton = true;
        //display search scope (if more than one view is configured)
        wp.DisplaySearchScope = false;
        //max number of items for multi choice filters
        wp.MaxNumberOfItems = 500;
        //display performance notification for administrators
        wp.DisplayPerformanceNotification = false;
        //number of columns in the filter details
        wp.NumberOfColumns = 3;
        //Expand and collapse list views (if title row of the web part is displayed)
        wp.AllowExpandCollapse = true;
        //Display a button to apply the filters instead of filtering immediately.
        wp.DisplayFilterButton = false;
        //Display total number of items and items in the result set.
        wp.DisplayItemCount = false;
        //Applies only to managed metadata fields with "All possible values" selected. In this case the entire managed metadata tree will be displayed.
        wp.IncludeDescendants = false;
        //Only show results if a filter is applied (improves performance on initial load)
        wp.HideViewByDefault = false;
        //Distinct Lookup Values
        wp.DistinctLookupValues = false;
        //IsConfigured flag
        wp.IsConfigured = true;
        //add the web part to the page
        wpm.AddWebPart(wp, "Main", 0);
    }
}

This method can be called for example in the ListAdded method of a SPListEventReceiver to add a Filter Web Part at the top of every view if a new list is created.

/// 
/// A list was added.
/// 
public override void ListAdded(SPListEventProperties properties)
{
     base.ListAdded(properties);
     foreach (SPView v in properties.List.Views)
     {
        AddFilterWebPart(v);
     }
}
/// 
/// A list was added.
/// 
public override void ListAdded(SPListEventProperties properties)
{
     base.ListAdded(properties);
     foreach (SPView v in properties.List.Views)
     {
        AddFilterWebPart(v);
     }
}