.NET Tips and Tricks

Blog archive

Connecting One WebPart to Many in SharePoint

Given the number of default interfaces that SharePoint provides, it's conceivable that you might want to create a provider WebPart that several consumers could connect to simultaneously. To make that happen, you must specify in the connection method that multiple consumers are allowed.

After decorating your connection method with the ConnectionProvider attribute, set the attribute's AllowsMultipleConnections property to True. Here's an example of a connection method for a provider that supports the IWebPartTable interface:

<ConnectionProvider("First Name", _
       AllowsMultipleConnections:=True)> _
Public Function MyProviderMethod() As IMyWebPartTable
  Return Me
End Function

A provider with a ConnectionProvider method like this could drive several consumer WebParts on the same page. You can also mark a ConnectionConsumer as allowing multiple connections which would allow a single consumer to interact with several providers at once (that would, I think, be something of a nightmare).

If you want a consumer WebPart to be able to connect to WebParts that support different interfaces (a consumer WebPart that works with both the IWebPartRow and IWebPartTable interfaces, for instance) then you need two separate connection methods: one for each interface. The same is true if you want to create provider that provides data through two or more interfaces (both the IWebPartRow and IWebPartParameters interfaces, for instance).

When you have multiple connection methods in the same WebPart, you must assign each method a unique id as the second parameter to the method's Connection* attribute. That's what this example does, creating a consumer that can work with both the IWebPartTable and IWebPartRow interfaces:

<ConnectionConsumer("Employee Data", "AllData")> _
Public Sub MyConsumerMethodForAllData(parm As IWebPartTable)
  '…code
End Sub
<ConnectionConsumer("Employee Data", "SelectedRow")> _
Public Sub MyConsumerMethodForSelectedRow(parm As IWebPartRow)
  '…code
End Sub

However, there are two scenarios related to these examples that aren't supported: A WebPart cannot connect multiple times to the same WebPart; if you have two WebParts that both support both the IWebPartTable and the IWebPartField interfaces (one as a consumer and one as a provider), they can only be connected once. The user will decide at runtime which interface to use.

Also, users can't connect a provider to a consumer that it's already connected to even if they swap roles. You can't, for instance, connect a WebPart A as a consumer to WebPart B as a provider, then turn around and have WebPart B connect as a consumer to WebPart A as a provider.

Posted by Peter Vogel on 12/10/2012


comments powered by Disqus

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube