Andrew Kennel

Web Developer

Making a connectible web part

August 25, 2008

For the last couple of days, I’ve been working on creating a custom web part to use as a filter.  I had no problems getting the web part created, and even reading in values from a Sharepoint list to populate a drop-down menu.  What I had trouble with was configuring my web part to act…

For the last couple of days, I've been working on creating a custom web part to use as a filter.  I had no problems getting the web part created, and even reading in values from a Sharepoint list to populate a drop-down menu.  What I had trouble with was configuring my web part to act as a connection provider.

All the examples I found via Google were written to create a Provider and Consumer web part and connect them to each other.  I couldn’t find anything that explained how to create a provider that could be connected to a generic list web part.  Finally today, I stumbled on a new approach, and was able to get my connection working with some help from D.

Here’s what I ended up with:

(snipped out a bunch of code to get to the good stuff)
/////////////////////////////////////////////////////////////////////////////////////////
//This First bit creates your connection provider and gives it a name
//////////////////////////////////////////////////////////////////////////////////////////
[ConnectionProvider(“Department”, “Text”)]
public ITransformableFilterValues SetConnectionInterface()
{
return this;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//The following section just lists out the various properties that your connection
//can have.  Most of them just return true or false.  Most are self-explanatory.
////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public bool AllowAllValue
{
get {return true ; }
}

        public bool AllowEmptyValue //This is really useful. It means that you don’t have to return a filter value if you don’t want to.
{
get { return true; }
}

        public bool AllowMultipleValues
{
get {return false; }
}

////////////////////////////////////////////////////////////////////////
//This is the name that appears in the settings window
//when you connect your web part to another web part
//on the page.  You can use whatever name you want
/////////////////////////////////////////////////////////////////////////       

public string ParameterName
{
get { return “Drop-down Menu Filter Value”; }
}

////////////////////////////////////////////////////////////////////////////////
//This is where we’re actually defining what value gets sent
//to the other web part via our connection.  In my case, I had
//a dropdown box called departmentPicker.  I’m sending the
//text value of the selected item.  (Notice that I run a quick check
// and if the value is set to the default “Select…” I just return null.
//
//I built this from an example that was returning multiple values.
//I’m only returning a single value, but I used their approach of
//returning a list which works fine for single values as well.  This
//way if I want to return multiple values in the future, I can reuse
//this code and not have to rewrite anything.
//////////////////////////////////////////////////////////////////////////////////       

public System.Collections.ObjectModel.ReadOnlyCollection ParameterValues
{
get
{
EnsureChildControls();
List department = new List();
if (departmentPicker.SelectedItem.Text == “Select…“)//If it is still set to the default, return null
{
return null;
}
else
{
department.Add(departmentPicker.SelectedItem.Text);//If it is set to any other value, add that value to our list
System.Collections.ObjectModel.ReadOnlyCollection result = new System.Collections.ObjectModel.ReadOnlyCollection(department);
return result; //return the filter value
}

            }
}

Share This Post