Export (0) Print
Expand All

IParametersInConsumer interface

NOTE: This API is now obsolete.

Allows a consumer Web Part to communicate its parameter list to other Web Parts.

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

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

The IParametersInConsumer part has the ability to pass initialization arguments to an IParametersInProviderpart and indicate which parameters are required. This allows the provider part to dynamically modify its user interface to better accommodate the necessary inputs for the consumer. Because the IParametersInConsumer can connect to both the IParametersOutProviderand the IParametersInProvider interfaces, it is a good choice as a universal receiver of parameters.

Also, server-side implementations of the IParametersInConsumer interface can be connected to parts on a different page by using an HTML editor compatible with Microsoft SharePoint Foundation, such as Microsoft SharePoint Designer. Connecting the IParametersInConsumer to IParametersInProvider is a direct connection, so no transformer dialog box is displayed. Connecting the IParametersInConsumer to IParametersOutProvider interface displays a transformer dialog box that allows an end user to map the parameter values between the parts. In this case, the consuming part doesn't need to be designed with knowledge of the provider because the end user is performing the mapping. However, this does require a user with a deeper understanding of the parts, and IParametersInConsumer can only be connected to IParametersOutProvider by an HTML editor compatible with SharePoint Foundation, such as SharePoint Designer.

The following code example shows a simple server-side IParametersInConsumer Web Part. It can be connected to another Web Part that implements the IParametersInProvider or IParametersOutProvider interfaces on the server. This Web Part displays a paragraph of text. The text style attributes such as font family, color, weight, and size can be set by a provider Web Part.

There are nine 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;

// 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: Reference the Communication namespace.
using Microsoft.SharePoint.WebPartPages.Communication;

namespace ConnectionCodeSamples
{
    // Step #2: Inherit from the WebPart base class and implement the 
    // IParametersInConsumer interface.
    public class ServerSideParametersInConsumer : WebPart, IParametersInConsumer
    {    
        // Step #3: Declare the IParametersInConsumer events.
        // Because this class implements the IParametersInConsumer 
        // interface, it must 
        // declare the interface member ParametersInConsumerInit.

        public event ParametersInConsumerInitEventHandler ParametersInConsumerInit;        

        // 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 set the text style of this Web Part connect it to an appropriate 

ParametersOut or ParametersIn Provider Web Part.";
        private string _missingInput = "is a required parameter input.";

        // Declare variables for Web Part user interface.
        private string _connectedWebPartLabel = "Connected to Web Part";
        private string _webPartDescription;
        string _fontFamily = string.Empty;    
        string _fontColor = string.Empty;        
        string _fontWeight = string.Empty;    
        string _fontSize = string.Empty;        
        bool _parametersInReadyFlag = false;
        bool _noParametersInFlag = false;

        // Declare variables for parameter attributes.
        string _fontFamilyParamDescription = "Font Family";
        string _fontFamilyParamDisplayName = "Font Family";
        string _fontFamilyParamName = "FFamily";
        string _fontColorParamDescription = "Font Color";
        string _fontColorParamDisplayName = "Font Color";
        string _fontColorParamName = "FColor";
        string _fontWeightParamDescription = "Font Weight";
        string _fontWeightParamDisplayName = "Font Weight";
        string _fontWeightParamName = "FWeight";
        string _fontSizeParamDescription = "Font Size";
        string _fontSizeParamDisplayName = "Font Size";
        string _fontSizeParamName = "FSize";

        // Constructor
        public ServerSideParametersInConsumer()
        {
            // Set Web Part description.
            _webPartDescription = "This is an example of a IParametersInConsumer Web Part.  The following ";
            _webPartDescription += "text style attributes can be set when passed as parameters via a Web Part Connection: ";
            _webPartDescription += "font color, weight, size, and family. This Web Part can only be connected to ";
            _webPartDescription += "another part which implements the IParametersOutProvider or IParametersInProvider interface. ";
            _webPartDescription += "A Windows SharePoint Services-compatible HTML editor is required to connect this Web Part to IParametersOutProvider.";
        }

        // Step #4: Override the EnsureInterfaces method and call 
        // RegisterInterface method.
        // The EnsureInterfaces method 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 IParametersInConsumer 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 IParametersInConsumer interface is allowed to 
                // connect across pages.</param>
                RegisterInterface("MyParametersInConsumerInterface",        //InterfaceName    
                    InterfaceTypes.IParametersInConsumer,                   //InterfaceType
                    WebPart.LimitOneConnection,                             //MaxConnections
                    ConnectionRunAt.Server,                                 //RunAtOptions
                    this,                                                   //InterfaceObject
                    "",                                                     //InterfaceClientReference
                    "Consume Font Parameters From",                         //MenuLabel
                    "Consumes font parameters from another 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 
        // 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 the connection state.
            if (interfaceName == "MyParametersInConsumerInterface")
            {
                _connected = true;
                _connectedWebPartTitle = SPEncode.HtmlEncode(connectedPart.Title);
            }
        }

        // Step #7: Override the PartCommunicationInit method.
        // The PartCommunicationInit method is called by the Web Part 
        // infrastructure during the ASP.NET PreRender 
        // phase 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 all controls have been created.
            EnsureChildControls();

            // Check if connected.
            if(_connected)
            {
                // If there is a listener, fire the 
                // ParametersInConsumerInit event.
                if (ParametersInConsumerInit != null)
                {
                    // Need to create the args for the 
                    // ParametersInConsumerInit event
                    ParametersInConsumerInitEventArgs parametersInConsumerInitEventArgs = new ParametersInConsumerInitEventArgs();
                    
                    //Set the ParameterInProperties
                    parametersInConsumerInitEventArgs.ParameterInProperties = new ParameterInProperty[4];

                    parametersInConsumerInitEventArgs.ParameterInProperties[0] = new ParameterInProperty();
                    parametersInConsumerInitEventArgs.ParameterInProperties[0].Description = _fontFamilyParamDescription;
                    parametersInConsumerInitEventArgs.ParameterInProperties[0].ParameterDisplayName = _fontFamilyParamDisplayName;
                    parametersInConsumerInitEventArgs.ParameterInProperties[0].ParameterName = _fontFamilyParamName;
                    parametersInConsumerInitEventArgs.ParameterInProperties[0].Required = true;

                    parametersInConsumerInitEventArgs.ParameterInProperties[1] = new ParameterInProperty();
                    parametersInConsumerInitEventArgs.ParameterInProperties[1].Description = _fontColorParamDescription;
                    parametersInConsumerInitEventArgs.ParameterInProperties[1].ParameterDisplayName = _fontColorParamDisplayName;
                    parametersInConsumerInitEventArgs.ParameterInProperties[1].ParameterName = _fontColorParamName;
                    parametersInConsumerInitEventArgs.ParameterInProperties[1].Required = false;

                    parametersInConsumerInitEventArgs.ParameterInProperties[2] = new ParameterInProperty();
                    parametersInConsumerInitEventArgs.ParameterInProperties[2].Description = _fontWeightParamDescription;
                    parametersInConsumerInitEventArgs.ParameterInProperties[2].ParameterDisplayName = _fontWeightParamDisplayName;
                    parametersInConsumerInitEventArgs.ParameterInProperties[2].ParameterName = _fontWeightParamName;
                    parametersInConsumerInitEventArgs.ParameterInProperties[2].Required = true;

                    parametersInConsumerInitEventArgs.ParameterInProperties[3] = new ParameterInProperty();
                    parametersInConsumerInitEventArgs.ParameterInProperties[3].Description = _fontSizeParamDescription;
                    parametersInConsumerInitEventArgs.ParameterInProperties[3].ParameterDisplayName = _fontSizeParamDisplayName;
                    parametersInConsumerInitEventArgs.ParameterInProperties[3].ParameterName = _fontSizeParamName;
                    parametersInConsumerInitEventArgs.ParameterInProperties[3].Required = false;

                    // Fire the ParametersInConsumerInit event.
                    ParametersInConsumerInit(this, parametersInConsumerInitEventArgs);
                }
            }
        }

        // Step #8: Implement the ParametersInReady event handler.
        // The connected provider part may call this method during its 
        // PartCommunicationMain phase
        // to pass its primary data to the consumer Web Part.
        // <param name="sender">Provider Web Part</param>
        // <param name="parametersInReadyEventArgs">The args passed by 
        // the Provider</param>
        public void ParametersInReady(object sender, ParametersInReadyEventArgs parametersInReadyEventArgs)
        {
            _parametersInReadyFlag = true;

            // Set the text box values to the values of the parameters.
            if(parametersInReadyEventArgs.ParameterValues != null)
            {
                _fontFamily = parametersInReadyEventArgs.ParameterValues[0];
                _fontColor = parametersInReadyEventArgs.ParameterValues[1];
                _fontWeight = parametersInReadyEventArgs.ParameterValues[2];
                _fontSize = parametersInReadyEventArgs.ParameterValues[3];

                // Store font attributes in a State Bag for use by the 
                //NoParametersIn event handler.
                ViewState["FontFamily"] = _fontFamily;
                ViewState["FontColor"] = _fontColor;
                ViewState["FontWeight"] = _fontWeight;
                ViewState["FontSize"] = _fontSize;
            }
        }

        // Step #9: Implement the NoParametersIn event handler.
        // The connected provider part may call this method during its 
        // PartCommunicationMain phase
        // to indicate there is no change in the parameter values. This 
        // allows the consumer part to 
        // display its cached data instead of potentially hitting a 
        // database again or recalculating values. 
        // <param name="sender">Provider Web Part</param>
        // <param name="eventArgs">The Event Argumentsr</param>
        public void NoParametersIn(object sender, EventArgs eventArgs)
        {
            _noParametersInFlag = true;

            //Set font attributes based on cached values
            _fontFamily = (string)ViewState["FontFamily"];
            _fontColor = (string)ViewState["FontColor"];
            _fontWeight = (string)ViewState["FontWeight"];
            _fontSize = (string)ViewState["FontSize"];
        }

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

            // Set text style attributes.
            output.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, _fontFamily);
            output.AddStyleAttribute(HtmlTextWriterStyle.Color, _fontColor);
            output.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, _fontWeight);
            output.AddStyleAttribute(HtmlTextWriterStyle.FontSize, _fontSize);

            // Formatted Web Part description.
            output.RenderBeginTag(HtmlTextWriterTag.Div);
            output.Write("<br>");
            output.Write(_webPartDescription);
            output.RenderEndTag();

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

            // Check if connected.
            if(_connected)
            {
                if (_fontFamily != null)
                {
                    // Indicate where parameters were retrieved from.
                    if (_parametersInReadyFlag)
                        output.Write("Parameters were retrieved from provider part.");
                    else if (_noParametersInFlag)
                        output.Write("Parameters were retrieved from <font color=blue>cache</font>.");
                }

                // 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();
                output.Write("<br>");

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

        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft