IFilterProvider interface

NOTE: This API is now obsolete.

Used to communicate with a Web Part that implements IFilterConsumer interface to provide filter information to the consumer.

Namespace:  Microsoft.SharePoint.WebPartPages.Communication
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

[ObsoleteAttribute("Use System.Web.UI.WebControls.WebParts.IWebPartParameters instead")]
public interface IFilterProvider

The IFilterProvider interface should be used with Web Parts that need to pass a collection of filter values to a consuming Web Part. The events of IFilterProvider were designed for the purpose of setting and clearing multiple filter values. It can be used in scenarios where the consumer part was designed with an understanding of the type of data passed by the provider part. Because the IFilterProvider part can receive initialization parameters from an IFilterConsumer part the IFilterProvider part can dynamically change its user interface to better fit the needs of the consumer part. The IFilterProvider can only be connected to the IFilterConsumer interface. When connecting the IFilterProvider to IFilterConsumer, this is a direct connection so no transformer dialog box will be displayed. Also, server-side implementations of IFilterProvider can be connected to parts on a different page by an HTML editor compatible with Microsoft SharePoint Foundation, such as Microsoft SharePoint Designer.

The following code example shows a server-side IFilterProvider Web Part. It can be connected to another Web Part which implements the IFilterConsumer interface on the server. This IFilterProvider Web Part will dynamically generate an input form based on the columns sent to it by the IFilterConsumer Web Part. The IFilterProvider Web Part can be used to pass filter values to the IFilterConsumer Web Part.

There are 8 steps specific to making this a connectable Web Part. These steps are numbered and commented in the following code example.

For more information on creating a connectable Web Part, see Creating a Connectable Web Part.

// Common .NET required namespaces
using System;
using System.ComponentModel;
using System.Web.UI;

// Web Part required namespaces
using Microsoft.SharePoint.WebPartPages;
using System.Xml.Serialization;
using System.Web.UI.WebControls;

// Code Access Security namespaces
using System.Security;
using Microsoft.SharePoint.Utilities;

//Step #1: Make a reference to the Communication namespace
using Microsoft.SharePoint.WebPartPages.Communication;

namespace ConnectionCodeSamples
    //Step #2: Inherit from the WebPart base class and implement the 
    // IFilterProvider interface.
    public class ServerSideFilterProvider : WebPart, IFilterProvider
        // Step #3: Declare the IFilterProvider events.
        // Because this class implements the IFilterProvider interface, 
        // it must declare the interface members SetFilter, 
        // ClearFilter, NoFilter. 
        public event SetFilterEventHandler SetFilter;
        public event ClearFilterEventHandler ClearFilter;
        public event NoFilterEventHandler NoFilter;        

        // Declare variables for keeping track of the connection state.
        private bool _connected = false;
        private string _connectedWebPartTitle = string.Empty;
        private string _registrationErrorMsg = "An error has occurred trying to register your connection interfaces.";
        private bool _registrationErrorOccurred = false;
        private string _notConnectedMsg = "NOT CONNECTED. To use this 
           Web Part connect it to a Filter Consumer Web Part.";

        // Declare variables for the Web Part user interface.
        private string _connectedWebPartLabel = "Connected to Web 
        private string _filterExpressionLabel = "FilterExpression 
        private string _fieldListKey = "ProviderFieldList"; 
        //Key for ViewState StateBag
        private string _filterExpression = string.Empty;
        private Button _setFilterButton;
        private Button _clearFilterButton;
        private Button _noFilterButton;
        private TextBox[] _filterInputs;
        // Declare variables for tracking whether the button was 
        // clicked.
        private bool _setFilterClicked = false;
        private bool _clearFilterClicked = false;
        private bool _noFilterClicked = false;

        // Declare variables for filter information.
        private string[] _fieldList  = null;
        private string[] _fieldDisplayList = null;
        private string FieldLabel = "FilterField";
        private string ValueLabel = "FilterValue";
        // Step #4: Implement EnsureInterfaces method and call 
        // RegisterInterface method.
        public override void EnsureInterfaces()
            // If your Web Part is installed in the bin directory and 
            // the Code Access Security (CAS) setting doesn't 
            // allow Web Part Connections, an exception will be thrown. 
            // To allow your Web Part to behave 
            // well and continue working, a try/catch block should be 
            // used when attempting to register interfaces.
            // Web Part Connections will only work if the level 
            // attribute of the <trust> tag in the 
            // web.config file is set to WSS_Minimal, WSS_Medium, or 
            // Full. By default a new SharePoint site
            // is installed with the trust level set to WSS_Minimal.
                // Register the IFilterProvider interface.
                // <param name="interfaceName">Friendly name of the 
                // interface that is being implemented.</param>
                // <param name="interfaceType">Specifies which 
                // interface is being implemented.</param>
                // <param name="maxConnections">Defines how many times 
                // this interface can be connected.</param>
                // <param name="runAtOptions">Determines where the 
                // interface can run.</param>
                // <param name="interfaceObject">Reference to the 
                // object that is implementing this interface.</param>
                // <param name="interfaceClientReference">Name used to 
                // reference the interface on the client. 
                // This is a server-side example so the value is set to 
                // empty string.</param>
                // <param name="menuLabel">Label for the interface 
                // tht appears in the UI</param>
                // <param name="description">Description of the 
                // interface that appears in the UI</param>
                // <param name="allowCrossPageConnection">Specifies if 
                // the interface can connect to a Web Part
                // on a different page. This is an optional parameter 
                // with a default of false. Note that only some 
                // server-side interfaces are allowed to connect across 
                // pages by the Web Part infrastructure. 
                // The IFilterProvider interface is allowed to connect 
                // across pages.</param>
                RegisterInterface("MyFilterProviderInterface",                //InterfaceName    
                    InterfaceTypes.IFilterProvider,                           //InterfaceType
                    WebPart.LimitOneConnection,                               //MaxConnections
                    ConnectionRunAt.Server,                                   //RunAtOptions
                    this,                                                     //InterfaceObject
                    "",                                                       //InterfaceClientReference
                    "Provide Filter To",                                      //MenuLabel
                    "Provides a Filter to a consumer Web Part.",              //Description
                    true);                                                    //allowCrossPageConnection
            catch(SecurityException se)
                _registrationErrorOccurred = true;

        // Step #5: Override the CanRunAt() method.
        // The CanRunAt method is called by the Web Part infrastructure 
        // during the ASP.NET PreRender event
        // to determine where the Web Part can run based on its current 
        // configuration.
        public override ConnectionRunAt CanRunAt()
            // This Web Part can run on the server.
            return ConnectionRunAt.Server;

        // Step #6: Override the PartCommunicationConnect() method.
        // The PartCommunicationConnect method is called by the Web 
        // Part infrastructure to notify the Web Part that it
        // is connected during the ASP.NET PreRender event. Relevant 
        // information is passed to the part such as 
        // the interface it is connected over, the Web Part it is being 
        // conected to, and where the part will be running, 
        // either client or server-side. 
        // <param name="interfaceName">Friendly name of the interface 
        // that is being connected</param>
        // <param name="connectedPart">Reference to the other Web Part 
        // that is being connected to</param>
        // <param name="connectedInterfaceName">Friendly name of the 
        // interface on the other Web Part</param>
        // <param name="runAt">Where the interface should 
        // execute</param>
        public override void PartCommunicationConnect(
            string interfaceName,
            WebPart connectedPart,
            string connectedInterfaceName,
            ConnectionRunAt runAt)
            // Keep track of whether the Web Part is connected.
            if (interfaceName == "MyFilterProviderInterface")
                _connected = true;
                _connectedWebPartTitle = 

        // Step #7: Override the PartCommunicationMain() method.
        // The PartCommunicationMain method is called by the Web Part 
        // infrastructure on the client during the ASP.NET PreRender
        // event to allow the part to pass its primary data to the 
        // other connected parts.
        // It is important to always fire either the SetFilter, 
        // NoFilter, or ClearFilter event. Some parts
        // may not behave properly if they are left waiting for this 
        // information.
        // SetFilter should be fired to send the filter expression.
        // NoFilter should be fired to indicate that there is no change 
        // in the filter expression.
        // ClearFilter should be fired to indicate that the filter 
        // should be removed.
        public override void PartCommunicationMain()
            // Ensure that all of the Web Part's controls are created.

            // Check if connected.
                // Check which button was clicked.
                if(_setFilterClicked == true)
                    // If there is a listener, fire the SetFilter 
                    // event.
                    if (SetFilter != null)
                        // Create the SetFilterEventArgs object for the 
                        // SetFilter event.
                        SetFilterEventArgs setFilterEventArgs = new 

                        // Create the filter expression.
                        int eIndex = 1;
                        string filterExpression = string.Empty;
                        for (int index = 0; index < _fieldList.Length; 
                            // This filter expression syntax should be 
                            // used with SharePoint lists
                            // and other Microsoft Web Parts that 
                            // support the IFilterConsumer interface.
                            if (_filterInputs[index].Text != 
                                filterExpression += FieldLabel + 
                                   eIndex.ToString() + "=" + 
                                   _fieldList[index] + "&";
                                filterExpression += ValueLabel + 
                                   eIndex.ToString()  + "=" + 
                                   _filterInputs[index].Text + "&";

                        // Trim Off Trailing '&'
                        if (filterExpression.Length != 0)
                            filterExpression = filterExpression.Substring(0,filterExpression.Length - 1);

                        // Set the FilterExpression property on the 
                        // SetFilterEventArgs object.
                        setFilterEventArgs.FilterExpression = 

                        // Set the _filterExpression variable for 
                        // display in the user interface.
                        _filterExpression = filterExpression;

                        // Fire the event.
                        SetFilter(this, setFilterEventArgs);

                        _setFilterClicked = false;
                else if(_clearFilterClicked == true)
                    // If there is a listener, fire the ClearFilter 
                    // event.
                    if (ClearFilter != null)
                        ClearFilter(this, new EventArgs());

                        _clearFilterClicked = false;

                        // Clear out values in input text boxes.
                        if (_filterInputs != null)
                            for (int index = 0; index < 
                               _filterInputs.Length; index++)
                               _filterInputs[index].Text = 
                else if(_noFilterClicked == true)
                    // If there is a listener, fire the NoFilter event.
                    if (NoFilter != null)
                        NoFilter(this, new EventArgs());

                        _noFilterClicked = false;
                    // If there is a listener, fire the NoFilter event.
                    if (NoFilter != null)
                        NoFilter(this, new EventArgs());

        // Step #8: Implement the FilterConsumerInit() method.
        // The connected consumer part will call this method during its 
        // PartCommunicationInit phase
        // to pass initialization information to the provider Web Part. 
        // The column names from the
        // consumer Web Part are passed in. In this example, these 
        // values are used to dynamcially 
        // generate the input text boxes in the provider Web Part.
        // <param name="sender">Consumer Web Part</param>
        // <param name="filterConsumerInitArgs">The args passed by the 
        // Consumer</param>
        public void FilterConsumerInit(object sender, 
           FilterConsumerInitEventArgs filterConsumerInitEventArgs)
            if(filterConsumerInitEventArgs.FieldList != null)
                _fieldList = filterConsumerInitEventArgs.FieldList;
                _fieldList = null;

            if(filterConsumerInitEventArgs.FieldDisplayList != null)
                _fieldDisplayList = filterConsumerInitEventArgs.FieldDisplayList;
                _fieldDisplayList = null;


        protected override void RenderWebPart(HtmlTextWriter output)
            // Check for connection interface registration error.
            if (_registrationErrorOccurred)

            // Check if connected.
                // Line break.

                // Generate input text boxes.
                string fieldName;
                for (int index = 0; index < _fieldDisplayList.Length; 
                    fieldName = _fieldDisplayList[index];
                    output.Write(fieldName + ": ");
                    output.RenderEndTag(); //End </B>
                    output.RenderEndTag(); //End </TD>

                    _filterInputs[index].RenderControl(output); //Render text box
                    output.RenderEndTag(); //End </TD>
                    output.RenderEndTag(); //End </TR>
                output.RenderEndTag(); //End <Table>

                // Line break..

                // Render the buttons.

                // Line break.

                // Render the connected Web Part title.
                output.Write(_connectedWebPartLabel + ": ");

                // Render the filter expression.
                output.Write(_filterExpressionLabel + ": ");

                // The Web Part isn't connected.

        // Create the Web Part user interface controls.
        protected override void CreateChildControls()
            // Create the buttons.
            _setFilterButton = new Button();
            _setFilterButton.ID = "SetFilterButton";
            _setFilterButton.Text = "Fire SetFilter";

            _clearFilterButton = new Button();
            _clearFilterButton.ID = "ClearFilterButton";
            _clearFilterButton.Text = "Fire ClearFilter";

            _noFilterButton = new Button();
            _noFilterButton.ID = "NoFilterButton";
            _noFilterButton.Text = "Fire NoFilter";

            // Create the input field text boxes.
            // The field names provided by the consumer Web Part needed 
            // to be stored in a StateBag because 
            // the page doesn't have the _fieldList in time after the 
            // first postback to create the text box 
            // controls and restore their view state.
            string providerFieldList = 
            int fieldCount;
            if (providerFieldList == null && _fieldList != null)
                // First postback of the page.
                // Generate controls from _fieldList provided by 
                // consumer Web Part.
                string[] FieldList = _fieldList;
                fieldCount = FieldList.Length;
                _filterInputs = new TextBox[fieldCount];
                for (int index = 0; index < fieldCount; index++)
                    _filterInputs[index] = new TextBox();
                    _filterInputs[index].ID = FieldList[index];

                    // Populate ViewState providerFieldList item to 
                    // keep track of field names.
                    if (index < fieldCount - 1)
                        ViewState[_fieldListKey] += 
                           FieldList[index].ToString() + ";";
                        ViewState[_fieldListKey] += 
            else if (providerFieldList != null)
                // Subsequent postback of page. 
                // Retrieve the field names from StateBag 
                // providerFieldList
                // Need to parse the providerFieldList information to 
                // get the individual fields
                string[] FieldList = providerFieldList.Split(new Char[] 
                fieldCount = FieldList.Length;

                _filterInputs = new TextBox[fieldCount];
                for (int index = 0; index < fieldCount; index++)
                    _filterInputs[index] = new TextBox();
                    _filterInputs[index].ID = FieldList[index];

            // Add event handler to listen for button's Click event.
            _setFilterClicked = false; // Initialize to false -- user 
               hasn't clicked yet
            _setFilterButton.Click += new 
            // listen for Button's click event

            _clearFilterClicked = false; // Initialize to false -- user 
               hasn't clicked yet
            _clearFilterButton.Click += new 
           // listen for Button's click event
            _noFilterClicked = false; // Initialize to false -- user 
               hasn't clicked yet
            _noFilterButton.Click += new 
            // listen for Button's click event

        // The button OnClick event handler.
        // <param name="sender">The Button object</param>
        // <param name="e">The Event Arguments</param>
        private void SetFilterButtonClicked(object sender, EventArgs e)
            _setFilterClicked = true; //user clicked button, set to 

        private void ClearFilterButtonClicked(object sender, EventArgs e)
            _clearFilterClicked = true; //user clicked button, set to 
        private void NoFilterButtonClicked(object sender, EventArgs e)
            _noFilterClicked = true; //user clicked button, set to true