1
votes

I'm prototyping the best way to dynamically connect web parts at runtime. Essentailly, the application will allow for several disparate application groups to create web parts that will be consumed within the SharePoint front end. All of the web parts will need to automatically detect consumers and providers to create connections at runtime.

What we are looking to do is have webparts discover and automatically connect to other compatible webparts. When a user adds the two compatible parts to a page, the parts should be able create the appropriate connections. We have a well defined interface for passing data between the parts, so the only issue is how to manage the connections. To be clear, we do not want user's to worry about having to create connections themselves.

For our purposes "best way" means most efficient, elegant and/or standard. We'd like to follow established sharepoint design patterns as much as possible, but code efficiency is somewhat important.

I've been able to draft a proof of concept that uses a base web part class to do this during the oninit event of each subclassed webpart. The oninit event grabs the current page's SPWebPartManager and itereates through each part creating consumer and provider connections for every webpart inheriting from the base class:

SPWebPartManager spManager = SPWebPartManager.GetCurrentWebPartManager(Page) as SPWebPartManager;
foreach (BaseWebPart provider in parts)
{
    foreach (BaseWebPart consumer in parts)
    {
        if (provider != consumer)
        {
            string connectionId = string.Format("WebPartConnection{0}{1}", consumer.ID, provider.ID);
            SPWebPartConnection conn = spManager.SPWebPartConnections[connectionId];
            if (conn == null)
            {
                conn = new SPWebPartConnection()
                {
                    ID = connectionId,
                    ConsumerID = consumer.ID,
                    ConsumerConnectionPointID = "WebPartConnectableConsumer",
                    ProviderID = provider.ID,
                    ProviderConnectionPointID = "WebPartConnectableProvider"
                };
                spManager.SPWebPartConnections.Add(conn);
            }
        }
    }
}
1

1 Answers

1
votes

I will highly recommend that you reconsider and then drop this idea.

I know that it may be hard to teach all users to connect web parts, and you might get your functionallity to work in limited scenarios.

But in more complicated scenarios you're just asking for trouble and you're limiting the possiblities of advanvced users.

  • If you implement a (or multiple) web part, which can consume and provide the same interface. Then put two of these on a page => Endless loop
  • If your user puts two providers and two consumers on the same page you have no way of pairing these as the user wants.
  • ...

My recommendation is that you develop your web parts so they can work both with and without connections (maybe hiding part of the UI if connected) and teach your users to use connections

Or you could go halfway and when showing your webpart in design mode, list the web parts it can be connected to as links, which the user can click to make connections.