Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
IListProvider Interface
Collapse the table of content
Expand the table of content

IListProvider Interface

NOTE: This API is now obsolete.

Defines events that a Web Part can implement so that it can provide an entire list (rowset) of data to another Web Part that implements the IListConsumer interface.

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

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

The IListProvider interface should be implemented in Web Parts that need to pass a collection of data that can be characterized as a list, such as a table of data. It can be used in scenarios where the consumer part was designed with an understanding of the data being sent by the provider. Also, a Web Part that implements the IListProvider interface has the ability to pass initialization arguments to the consumer part. Connecting the IListProvider interface to the IListConsumer interface is a direct connection, so no transformer dialog is displayed.

The following code example shows a simple server-side IListProvider Web Part. It can be connected to one or more Web Parts that implement the IListConsumer interface on the server. This example displays a list of products. When the Fire ListReady button is clicked, this Web Part passes the entire list of products to all other Web Parts that are connected to it.

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

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

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

// DataGrid and user interface namespaces
using System.Data;
using System.Drawing;

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

//Step #1: Reference the Communication namespace.
using Microsoft.SharePoint.WebPartPages.Communication;


namespace ConnectionCodeSamples
{
    //Step #2: Inherit from the WebPart base class and implement the 
    //IListProvider interface.
    public class ServerSideListProvider : WebPart, IListProvider
    {    
        
        // Step #3: Declare variables for IListProvider events.
        // Because this class implements the IListProvider interface, 
        // it must declare the interface members ListProviderInit, 
        // ListReady, PartialListReady. 
        
        public event ListProviderInitEventHandler ListProviderInit;
        public event ListReadyEventHandler ListReady;
        public event PartialListReadyEventHandler PartialListReady; 

        // Declare variables for keeping track of 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 List Consumer Web Part.";

        // Declare variables for Web Part user interface.
        private string _connectedWebPartLabel = "Connected to Web Part";    
        private DataGrid _dataGrid = new DataGrid();
        private bool _listButtonClicked = false;
        private Button _listButton;
        
        // Declare variables for list information.
        private string[] _listFieldDisplayNames;
        private string[] _listFieldNames;
        
        
        // Step #4: Override EnsureInterfaces method and call RegisterInterface method.
        // EnsureInterfaces() is called by the Web Part Infrastructure during the ASP.NET PreRender event 
        // and allows the part to register all of its connection interfaces.
        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.
            try
            {
                // Register the IListProvider 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 
                // which appears in the UI</param>
                // <param name="description">Description of the 
                // interface which 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 IListProvider interface is not allowed to go 
                // cross page.</param>
                RegisterInterface("MyListProviderInterface",                //InterfaceName    
                    InterfaceTypes.IListProvider,                           //InterfaceType
                    WebPart.UnlimitedConnections,                           //MaxConnections
                    ConnectionRunAt.Server,                                 //RunAtOptions
                    this,                                                   //InterfaceObject
                    "",                                                     //InterfaceClientReference
                    "Provide List To",                                      //MenuLabel
                    "Provides a list to a consumer Web Part.");             //Description
            }
            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 
        // connected 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 connection state.
            if (interfaceName == "MyListProviderInterface")
            {
                _connected = true;
                _connectedWebPartTitle = SPEncode.HtmlEncode(connectedPart.Title);
            }
        }

        // Step #7: Override PartCommunicationInit method.
        // PartCommunicationInit() is called by the Web Part 
        // infrastructure during the ASP.NET PreRender 
        // event to allow the part to pass initialization information 
        // to the other connected parts.
        // It is important to always pass initialization information. 
        // Some parts may not behave properly if this initialization 
        // information is not received.
        
        public override void PartCommunicationInit()
        {
            // Ensure that all of the Web Part's controls are created.
            EnsureChildControls();

            // Check if connected.
            if(_connected)
            {
                // If there is a listener, fire ListProviderInit event.
                if (ListProviderInit != null)
                {
                    //Create the ListProviderInitEventArgs object for 
                    //the ListProviderInit event.
                    ListProviderInitEventArgs listProviderInitArgs = new ListProviderInitEventArgs();
                    
                    // Set the list field names.
                    listProviderInitArgs.FieldList = _listFieldNames;
                    listProviderInitArgs.FieldDisplayList = _listFieldDisplayNames;

                    // Fire the ListProviderInit event.
                    ListProviderInit(this, listProviderInitArgs);
                }
            }
        }

        // Step #8: 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 the ListReady or 
        // PartialListReady event. Some parts
        // may not behave properly if they are left waiting for this 
        // information.
        public override void PartCommunicationMain()
        {
            // Ensure that all of the Web Part's controls are created.
            EnsureChildControls();

            //Check if connected
            if(_connected)
            {
                // If there is a listener, fire the ListReady event.
                if (ListReady != null)
                {
                    // Create the ListReadyEventArgs object for the 
                    // ListProviderInit event.
                    ListReadyEventArgs listReadyArgs = new ListReadyEventArgs();

                    // If user clicked button, send the value.
                    if (_listButtonClicked)
                    {
                        // Set the List to the value of the table in 
                        // the DataGrid.
                        // This is the value that will be sent to the 
                        // consumer Web Part.
                        listReadyArgs.List = ((DataTable)_dataGrid.DataSource);
                    }
                    else
                    {
                        // The user didn't click the button, so send a
                        // null DataTable to the consumer Web Part.
                        listReadyArgs.List = null;
                    }

                    // Fire the ListReady event.
                    // The consumer Web Part will receive the 
                    // DataTable.
                    ListReady(this, listReadyArgs);
                }
            }
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
            // Check for connection interface registration error.
            if (_registrationErrorOccurred)
            {
                output.Write(_registrationErrorMsg);
                return;
            }

            // Ensure that all of the Web Part's controls are created.
            EnsureChildControls();

            // Check if connected.
            if(_connected)
            {
                // Line break.
                output.RenderBeginTag(HtmlTextWriterTag.Br);
                output.RenderEndTag();

                // Render the DataGrid control.
                _dataGrid.RenderControl(output);

                // Render the Button control.
                _listButton.RenderControl(output);

                // Line break.
                output.RenderBeginTag(HtmlTextWriterTag.Br);
                output.RenderEndTag();

                // Render connected Web Part title.
                output.Write(_connectedWebPartLabel + ": ");
                output.RenderBeginTag(HtmlTextWriterTag.I);
                output.Write(_connectedWebPartTitle);
                output.RenderEndTag();

            }
            else
            {
                // The Web Part isn't connected.
                output.Write(_notConnectedMsg);
            }
        }

        // Create Web Part user interface controls.
        protected override void CreateChildControls()
        {
            // Create the Button control.
            _listButton = new Button();
            _listButton.ID = "ListButton";
            _listButton.Text = "Fire ListReady";
            Controls.Add(_listButton);

            // Add event handler to listen for button Click event.
            _listButtonClicked = false; // Initialize to false -- user hasn't clicked yet
            _listButton.Click += new EventHandler(ListButtonClicked); // listen for Button's click event
            
            // Create the DataTable.
            DataTable dataTable = new DataTable();

            // Add four column objects to the table.
            DataColumn idColumn = new  DataColumn();
            idColumn.DataType = System.Type.GetType("System.Int32");
            idColumn.ColumnName = "ID";
            idColumn.Caption = "ID";
            idColumn.AutoIncrement = true;
            dataTable.Columns.Add(idColumn);

            DataColumn Product = new DataColumn();
            Product.DataType = System.Type.GetType("System.String");
            Product.ColumnName = "Product";
            Product.Caption = "Product";
            dataTable.Columns.Add(Product);

            DataColumn productCategory = new DataColumn();
            productCategory.DataType = System.Type.GetType("System.String");
            productCategory.ColumnName = "Category";
            productCategory.Caption = "Category";
            dataTable.Columns.Add(productCategory);

            DataColumn Stock = new DataColumn();
            Stock.DataType = System.Type.GetType("System.Int32");
            Stock.ColumnName = "Stock";
            Stock.Caption = "Stock";
            dataTable.Columns.Add(Stock);

            // Once a table has been created, use the NewRow method to 
            // create rows.
            DataRow dataRow;
            
            // Add first row to the collection.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Aniseed Syrup";
            dataRow["Category"] = "Condiments";
            dataRow["Stock"] = 25;
            dataTable.Rows.Add(dataRow);

            // Add a second row
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Vegie-spread";
            dataRow["Category"] = "Condiments";
            dataRow["Stock"] = 10;
            dataTable.Rows.Add(dataRow);

            // Add a third row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Outback Lager";
            dataRow["Category"] = "Beverages";
            dataRow["Stock"] = 30;
            dataTable.Rows.Add(dataRow);

            // Add a fourth row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Boston Crab Meat";
            dataRow["Category"] = "Seafood";
            dataRow["Stock"] = 10;
            dataTable.Rows.Add(dataRow);

            // And a fifth row.
            dataRow = dataTable.NewRow();
            dataRow["Product"] = "Tofu";
            dataRow["Category"] = "Produce";
            dataRow["Stock"] = 41;
            dataTable.Rows.Add(dataRow);

            // Set the DataGrid's DataSource.
            _dataGrid.DataSource = dataTable;
            _dataGrid.ID = "DataGrid";

            // Format the DataGrid.
            _dataGrid.HeaderStyle.Font.Bold = true;
            _dataGrid.HeaderStyle.ForeColor = Color.DarkBlue;
            _dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            _dataGrid.AlternatingItemStyle.BackColor = Color.Beige;

            _dataGrid.DataBind();
            Controls.Add(_dataGrid);

            // Set the DataTable FieldName information.
            //This information will be passed to the Consumer by firing 
            // the ListProviderInit event.
            int columnCount = dataTable.Columns.Count;
            _listFieldNames = new string[columnCount];
            _listFieldDisplayNames = new string[columnCount];

            for(int i = 0; i < columnCount; i++)
            {
                _listFieldNames[i] = dataTable.Columns[i].ColumnName;
                _listFieldDisplayNames[i] = dataTable.Columns[i].Caption;
            }
        }

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


Show:
© 2015 Microsoft