Wednesday, February 23, 2011

Programmatically connect two ListViewWebParts to Filter based on value of Provider Web Part

When you want to connect two ListViewWebParts on a page using the user interface is easy. However doing it programmatically was a bit more complicated. It’s not complex, but you need to know what classes you need to use.

Connecting Web Parts using the User interface:

Select the provider Web Part and connect to the consumer web part with the ‘Send Row of Data To’ option.

Step 1:
Configure the connection on the Consumer Web Part.
Select ‘Get Filter Values From’


Step 2:
Provider Field Name: Title(linked to item)
Consumer Field Name: ArticleEditionTitle

The provider and consumer field names are dependent on your needs. In this case the value of the Title of the provider Web Part is passed to the consumer web part. The consumer web part will only show rows where ArticleEditionTitle is equal to the passed value.

 Connecting Web Parts programmatically:
Use the SPRowToParametersTransformer class, provide the correct provider and consumer fieldnames. These fields are the same as the ones used in step 2 of the user interface procedure.

SPRowToParametersTransformer transformer = new SPRowToParametersTransformer();
transformer.ProviderFieldNames = new string[] { "LinkTitleNoMenu" };
transformer.ConsumerFieldNames = new string[] { "ArticleEditionTitle" };

Get the WebParts that need to get connected
string providerWebPartTitle = "Editions";
string consumerWebPartTitle = "Articles";

WebPart providerPart = (from WebPart w in mgr.WebParts where w.Title == providerWebPartTitle select w).FirstOrDefault();
WebPart consumerPart = (from WebPart w in mgr.WebParts where w.Title == consumerWebPartTitle select w).FirstOrDefault();

Determine the ConnectionPoints of the webparts. Note the provider/consumer ConnectionIds
string providerConnectionId = "DFWP Row Provider ID";
string consumerConnectionId = "DFWP Filter Consumer ID";

//get connectionpoints
ProviderConnectionPoint providerConnectionPoint = (from ProviderConnectionPoint conn in manager.GetProviderConnectionPoints(providerPart)
where conn.ID == providerConnectionId
select conn).FirstOrDefault();

ConsumerConnectionPoint consumerConnectionPoint = (from ConsumerConnectionPoint conn in manager.GetConsumerConnectionPoints(consumerPart)
where conn.ID == consumerConnectionId
select conn).FirstOrDefault();

Add a WebPartConnection using WebPartManager.SPConnectWebParts, and pass along the transformer.

// connect the webparts
manager.SPConnectWebParts(providerPart, providerConnectionPoint, consumerPart, consumerConnectionPoint, transformer);

That’s it.


  1. Perfect. I have migrated a solution from SharePoint 2007 to 2010 that included connected web parts. It worked in 2007, but not on the 2010 platform. Your code, however, does. Thank you.

  2. how are you defining mgr?

  3. Hi,

    Does this work without the Enterprise version as I only have access to Standard?

  4. You should need to update your blog I think.