Step 1: Develop an ASP.NET Web Part Assembly

To create a simple filter consumer Web Part, you start by developing an ASP.NET Web Part assembly. You can create one typically by creating a class library with a class that derives from the System.Web.UI.WebControls.WebParts.WebPart base class.

For more information on developing ASP.NET assemblies, see the following ASP.NET 2.0 documentation:

  • ASP.NET Web Parts Quick Start

  • ASP.NET Web Parts Documentation

  • ASP.NET 2.0 Web Part Connections

Prerequisites

This topic assumes that you are using Microsoft Visual Studio 2005.

To develop an ASP.NET Web Part assembly

  1. Start Visual Studio 2005.

  2. Start a new C# class library project.

  3. Add a reference to System.Web, Microsoft.Office.Server, Microsoft.SharePoint, and Microsoft.SharePoint.Portal.

  4. In your .cs file, copy and paste in the following code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using aspnetwebparts = System.Web.UI.WebControls.WebParts;
    using Microsoft.Office.Server.Utilities;
    using wsswebparts = Microsoft.SharePoint.WebPartPages;
    using Microsoft.SharePoint.Portal.WebControls;
    using System.Collections.ObjectModel;
    using Microsoft.SharePoint.Utilities;
    using System.Data;
    using System.Collections;
    
    namespace MyWebPartLibrary
    {
        public class SimpleFilterConsumerWebPart : aspnetwebparts.WebPart
        {
    
            List<wsswebparts.IFilterValues> providers = new List<wsswebparts.IFilterValues>();
    
    
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
            }
    
    
            [aspnetwebparts.ConnectionConsumer("Simple Consumer", "IFilterValues", AllowsMultipleConnections = false)]
            public void SetConnectionInterface(wsswebparts.IFilterValues provider)
            {
                this.providers.Add(provider);
                if (provider != null)
                {
                    List<wsswebparts.ConsumerParameter> l = new List<wsswebparts.ConsumerParameter>();
                    l.Add(new wsswebparts.ConsumerParameter("Value", wsswebparts.ConsumerParameterCapabilities.SupportsMultipleValues | wsswebparts.ConsumerParameterCapabilities.SupportsAllValue));
                    provider.SetConsumerParameters(new ReadOnlyCollection<wsswebparts.ConsumerParameter>(l));
                }
            }
    
            protected override void RenderContents(HtmlTextWriter output)
            {
                this.EnsureChildControls();
                foreach (wsswebparts.IFilterValues provider in this.providers)
                {
                    if (provider != null)
                    {
                        string prop = provider.ParameterName;
                        ReadOnlyCollection<String> values = provider.ParameterValues;
    
                        if (prop != null && values != null)
                        {
                            output.Write("<div>" + SPEncode.HtmlEncode(prop) + ":</div>");
                            foreach (string v in values)
                            {
                                if (v == null)
                                {
                                    output.Write("<div>&nbsp;&nbsp;<i>&quot;(empty)&quot;/null</i></div>");
                                }
                                else if (v.Length == 0)
                                {
                                    output.Write("<div>&nbsp;&nbsp;<i>empty string</i></div>");
                                }
                                else
                                {
                                    output.Write("<div>&nbsp;&nbsp;" +v + "</div>");
                                }
                            }
                        }
                        else
                        {
                            output.Write("<div>No filter specified (all).</div>");
                        }
                    }
                    else
                    {
                        output.Write("<div>Not connected.</div>");
                    }
    
                    output.Write("<hr>");
                }
            }
    
        }
    }
    
  5. Compile the solution. Now you should have an assembly that contains your simple filter consumer Web Part.

Next Steps

Step 2: Place your Assembly in the Bin or Global Assembly Cache