I've created several master / detail webparts that need to be connected. We have a requirement the the webparts self discover and connect to other connectable webparts on the page. I've acheived this in a standard ASP.NET page with the following code:
protected override void OnLoad(EventArgs e)
{
WebPartManager manager = WebPartManager.GetCurrentWebPartManager(Page);
manager.StaticConnections.Add(new WebPartConnection()
{
ID = string.Format("WebPartConnection{0}{1}", this.ID, provider.ID),
ConsumerID = this.ID,
ConsumerConnectionPointID = "WebPartConnectableConsumer",
ProviderID = provider.ID,
ProviderConnectionPointID = "WebPartConnectableProvider"
});
}
This approach, however, does not work in SharePoint. Using the SharePoint version of these objects results in a generic sharepoint error:
protected override void OnLoad(EventArgs e)
{
SPWebPartManager spManager = SPWebPartManager.GetCurrentWebPartManager(Page) as SPWebPartManager;
spManager.StaticConnections.Add(new WebPartConnection()
{
ID = string.Format("WebPartConnection{0}{1}", this.ID, provider.ID),
ConsumerID = this.ID,
ConsumerConnectionPointID = "WebPartConnectableConsumer",
ProviderID = provider.ID,
ProviderConnectionPointID = "WebPartConnectableProvider"
});
}
The following approach works, but creates the connection as part of the user's personalization:
protected override void OnLoad(EventArgs e)
{
SPWebPartConnection connection = (from SPWebPartConnection c in spManager.SPWebPartConnections where c != null && c.Consumer == this && c.ConsumerConnectionPointID == "WebPartConnectableConsumer" && c.Provider == provider select c).FirstOrDefault();
if (connection == null)
{
try
{
ProviderConnectionPointCollection providerCollections = spManager.GetProviderConnectionPoints(provider);
ConsumerConnectionPointCollection consumerConnections = spManager.GetConsumerConnectionPoints(this);
connection = spManager.SPConnectWebParts(provider, providerCollections["WebPartConnectableProvider"], this, consumerConnections["WebPartConnectableConsumer"]);
}
catch { }
}
}