Wednesday, July 21, 2010

Custom Consumer Web Part

Retrieving data from another web part by means of connections is pretty simple. In the code example I will show you how you can create your own consumer web part that will allow you to recieve a row of data from another web part, custom or out of the box.

Code Example :

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Text;

namespace MarkAnthonyParker.ConsumsumerWebPart
{
[ToolboxItemAttribute(false)]
public class ConsumsumerWebPart: WebPart
{

private IWebPartRow _provider;
private DataRowView _tableRow;

protected override void RenderContents(HtmlTextWriter writer)
{
if (_tableRow != null)
{
// Add method to use data
}
}

private void GetRowData(object tableRowData)
{
_tableRow = (DataRowView)tableRowData;
}

protected override void OnPreRender(EventArgs e)
{
if (_provider != null)
{
_provider.GetRowData(new RowCallback(GetRowData));
}
}

[ConnectionConsumer("Row")]
public void SetConnectionInterface(IWebPartRow provider)
{
_provider = provider;
}

public class TableConsumerConnectionPoint : ConsumerConnectionPoint
{
public TableConsumerConnectionPoint(System.Reflection.MethodInfo callbackMethod, Type interfaceType, Type controlType, string name,
string id, bool allowMultipleConnections)
: base(callbackMethod, interfaceType, controlType, name, id, allowMultipleConnections)
{
}
}
}
}

This code can be used for both SharePoint 2007 and SharePoint 2010 web parts.

No comments:

Post a Comment