I have a couple of webparts on a page, XsltListViewWebPart and SPSlicerTextWebPart instances. I need to programmatically connect the filter (slicer) to the xslt webpart. When I do so, I need a transformer to transform the filtervalues to the correct format. I use the TransformableFilterValuesToParametersTransformer for this. The connect code is:
/// <summary>
/// Connects two webparts to each other
/// </summary>
/// <param name="consumerWebPart">The webpart which will consume the data</param>
/// <param name="consumerInterfaceType">The type of interface the consumer endpoint should have</param>
/// <param name="providerWebPart">The webpart which will provide the data</param>
/// <param name="providerInterfaceType">The type of interface the provider endpoint should have</param>
public void ConnectBusinessDataWebParts(
System.Web.UI.WebControls.WebParts.WebPart consumerWebPart,
string consumerConnectionPoint,
System.Web.UI.WebControls.WebParts.WebPart providerWebPart,
string providerConnectionPoint,
WebPartTransformer transformer)
{
// Get the connection point for the consumer.
System.Web.UI.WebControls.WebParts.ConsumerConnectionPointCollection consumerConnections =
wpmngr.GetConsumerConnectionPoints(consumerWebPart);
ConsumerConnectionPoint consumerConnection = null;
foreach (ConsumerConnectionPoint cpoint in consumerConnections)
{
if (cpoint.ID == consumerConnectionPoint)
{
consumerConnection = cpoint;
break;
}
}
// Get the connection point for the provider.
System.Web.UI.WebControls.WebParts.ProviderConnectionPointCollection providerConnections =
wpmngr.GetProviderConnectionPoints(providerWebPart);
ProviderConnectionPoint providerConnection = null;
foreach (ProviderConnectionPoint ppoint in providerConnections)
{
if (ppoint.ID == providerConnectionPoint)
{
providerConnection = ppoint;
break;
}
}
// when a transformer was used; create a new connection based on it; otherwise without one
if (transformer != null)
{
Microsoft.SharePoint.WebPartPages.SPWebPartConnection newConnection =
wpmngr.SPConnectWebParts(
providerWebPart, providerConnection,
consumerWebPart, consumerConnection,
transformer);
wpmngr.SPWebPartConnections.Add(newConnection);
}
else
{
Microsoft.SharePoint.WebPartPages.SPWebPartConnection newConnection =
wpmngr.SPConnectWebParts(
providerWebPart, providerConnection,
consumerWebPart, consumerConnection);
}
}
This gives me the following exception: The transformer type "Microsoft.SharePoint.WebPartPages.TransformableFilterValuesToParametersTransformer" is not allowed to be used on this page.
Which is weird, because when I connect the webparts through the interface it all works fine and an instance of the same TransformableFilterValuesToParametersTransformer is added. I also checked all properties and stuff, everything matches with what the GUI creates when I couple things in there.
Update 1: I used reflector to check what's going on inside. The problem seems to lie with the !base.AvailableTransformers.Contains(type)) check. That returns false becase the AvailableTransformers collection doesn't contain the transformer I need. This collection is created upon instantiation, when my page is still empty. The available transformers are read from the webparts which aren't there yet. After that, the collection doesn't seem to be refreshed when new webparts are added.
Update 2: Crap, doesn't work. A simple solutions seems to be reinstantiation of the entire SPLimitedWebPartManager object, which will force the collection to reread the webpart transformers. I'm going to try that next.
Update 3: sadly, I still don't have it working properly