There are many ways and complexities around developing connected web parts. Most times you just need a simple generic connected web part. With my project I was under a lot of time constraints and when I had to develop a connected web part, most articles and examples I found went into too much detail. Not that it is a bad thing, but I just had to get it done. So here is my quick simple connected web part.
The long and the short of it is that your web part needs to interface to other web parts. This interface is basically the link between the web parts: as in when you select Connections from the Edit menu on the web part:
In order enable a connected web part (apart from actually creating a web part in Visual Studio – which I will not go into here) you need two steps:
- Create an interface class to be used by your web part
- Use the interface class in your web part to get the linked value
Step 1: Creating the interface class
Here is a simple interface class, note that it inherits from ConsumerConnectionPoint. Ensure you have a reference “Imports System.Reflection”. Also replace YourWebPart below with the name of you web part class:
''' <summary>
''' This class provides a generic connection to any other web part field.
''' </summary>
Public Class FieldConsumerConnectionPoint
Inherits ConsumerConnectionPoint
Public Sub New(ByVal callbackMethod As MethodInfo, _ ByVal interfaceType As Type, ByVal controlType As Type, _ ByVal name As String, ByVal id As String, _ ByVal allowsMultipleConnections As Boolean) MyBase.New(callbackMethod, interfaceType, controlType, _ name, id, allowsMultipleConnections) End Sub
Public Overrides Function GetEnabled(ByVal control As Control) _
As Boolean
Return CType(control, YourWebPart).ConnectionPointEnabled
End Function
End Class
Step 2: Implement the interface class the web part
In the the example code below my connection was focussed on retrieving a numeric value. I have grouped the interfacing code into a region so I can easily reuse it in other web parts. The function GetConnectionValue returns -1 if the web part is not connected, or the value from the connected web part. Here’s the interface region:
#Region "Web Part Connection Interface"
''' <summary>
''' Returns the ID of the current connection.
''' </summary>
''' <remarks>Returns -1 if the web part is not connected.</remarks>
''' <returns>Integer</returns>
Function GetConnectionValue() As Integer
Dim returnID As Integer = 0
'check if there is a valid connection:
If _provider Is Nothing Then Return -1
'check connection & get field value:
If Not (_provider Is Nothing) Then
_provider.GetFieldValue(New FieldCallback(AddressOf GetFieldValue))
End If
'get the the value of the connected field:
Dim prop As PropertyDescriptor = _provider.Schema
If Not (prop Is Nothing) AndAlso Not (_fieldValue Is Nothing) Then
Try
returnID = CInt(CStr(_fieldValue))
Catch ex As Exception
End Try
End If
Return returnID
End Function
''' <summary>
''' The connection provider.
''' </summary>
''' <remarks>is set to nothing when no connection exits.</remarks>
Private _provider As IWebPartField
''' <summary> ''' The value of the connected field.
''' </summary>
''' <remarks>Returns nothing when no connection exists.</remarks>
Private _fieldValue As Object
''' <summary>
''' Retrieves the field value from the connected web part.
''' </summary>
Private Sub GetFieldValue(ByVal fieldValue As Object)
_fieldValue = fieldValue
End Sub
Public Property ConnectionPointEnabled() As Boolean
Get
Dim o As Object = ViewState("ConnectionPointEnabled")
If Not (o Is Nothing) Then
Return CBool(o)
Else
Return True
End If
Get
Set(ByVal value As Boolean)
ViewState("ConnectionPointEnabled") = value
End Set
End Property
<ConnectionConsumer("FieldConsumer", "Connpoint1", _
GetType(FieldConsumerConnectionPoint), AllowsMultipleConnections:=True)> _
Public Sub SetConnectionInterface(ByVal provider As IWebPartField)
_provider = provider
End Sub
#End Region
(Apologies for the poor formatting – still getting used to the online editor PLUS I also wanted to paste the text so it can be easily copied)
Now you can implement the connection, for example:
appID = GetConnectionValue()
Select Case appID
Case Is = -1 'not connected
Title.Text = "<b>Not Connected</b><br>"
Case Is = 0 'nothing selected
Title.Text = "<font size=""+1"">Nothing Selected</font><br>"
Case Else
'use the value here
End Select
And that’s about it: quick and dirty to get you going fast!
