Aug 2, 2009

WebPart Connections With Power WebPart 3.0

Web Part connections in SharePoint allows you to connect different Web Parts and to exchange data between them, even if they are from different suppliers. This is possible through the ASP.NET WebPart Connection Framework and some interfaces. For sure Power WebPart supports Web Part connections. It can act either as a row and table provider or as a consumer.

image

The example shows on the left a Power WebPart using a SPGridView to display current processes and acts as a row provider. It uses a DataTable as data source and sends the selected DataRow to the consumers. Theoretically you can send any object, but PowerShell dynamic types/properties (add-member) behave different and can not be reflected by the Web Part Connection Framework, so I recommend using DataTables and DataRows for WebPart connections ( in PowerShell 2.0 you can alternatively use add-type to generate C# types on the fly). On the right you see two consumers, a Power WebPart and a SharePoint List View WebPart. Both consume the DataRow from the provider on the left. The Power WebPart consumer simply shows the first column and the SharePoint List View uses the “Name” column of the provided row to filter the list items.

A script says more than thousand words.

The row provider web part:

# SPGridView to display the result 
$gridView = New-Object Microsoft.SharePoint.WebControls.SPGridView

# DataTable is used as data container
$table = New-Object System.Data.DataTable("Container")

function CreateChildControls($controls)
{ 
# add columns to the table
$table.Columns.Add($(New-Object System.Data.DataColumn("ID",[Int32])))
$table.Columns.Add($(New-Object System.Data.DataColumn("Name",[String])))
$table.Columns.Add($(New-Object System.Data.DataColumn("CPU",[double])))

# fill the table with processes
foreach($p in $(get-process))
{
$row = $table.NewRow()
$row.ID = $p.ID
$row.Name = $p.Name
$row.CPU = [double]$p.CPU

$table.Rows.Add($row)
}

# add columns to the SPGridView
$colSelectButton = New-Object System.Web.UI.WebControls.CommandField
$colSelectButton.HeaderText = "Action"
$colSelectButton.SelectText = "select"
$colSelectButton.ShowSelectButton = $true
$gridView.Columns.Add($colSelectButton)


$colId = New-Object Microsoft.SharePoint.WebControls.SPBoundField
$colId.HeaderText = "ID"
$colId.DataField = "ID"
$gridView.Columns.Add($colId)

$colName = New-Object Microsoft.SharePoint.WebControls.SPBoundField
$colName.HeaderText = "Name"
$colName.DataField = "Name"
$gridView.Columns.Add($colName)

$colCPU = New-Object Microsoft.SharePoint.WebControls.SPBoundField
$colCPU.HeaderText = "CPU"
$colCPU.DataField = "CPU"
$gridView.Columns.Add($colCPU);

# configure SPGridView
$gridView.AutoGenerateColumns=$false
$gridView.DataSource = $table
$gridView.DataBind()

$controls.Add($gridView)
}

# returns the schema to the WP Connection Framework
function GetSchema
{  
# return an empty row (or type, used for reflection)
return $table.NewRow()
}

# sends the selected row to the WP Connection Framework
function SendRow
{ 
if($gridView.SelectedIndex -gt -1) 
{
return $table.Rows[$gridView.SelectedIndex]
}
else
{
# return $null when nothing is selected
return $null
}
}

The row consumer web part:

$label = New-Object System.Web.UI.WebControls.Label

function OnReceiveRow($row, $schema)
{
if ($row -ne $null){$label.Text= $row[0]}
}

function OnPreRender
{ 
# request the row from the web part connection
# will fire "OnReceiveRow" 
Query-Connections 
}

function Render($writer)
{
$writer.Write("

") $label.RenderControl($writer) $writer.Write("

") }

WebPart Connections work with Power WebPart 3.0 build 0908020651 or greater.