Web Parts. Web parts connections.

by Kai 20. June 2011 04:06

Hi 2 all!

Next post about web parts is about web parts connections. All my post listed here:

How to create connections between two web parts and provide data? I’ve created simplest ever example just to illustrate how it works. Let’s connect our web parts!

First of all you must create public interface, which will act as data transfer between your web parts. So, let’s create it:

public interface IDataProvider
{
	string Name { get; }
}

As you can see It’s very simple in my way. Next step is implement two web parts- one is a provider and second is a consumer. Provider supply data to consumer, consumer gets data from provider and performs some processing of this data.

In provider web part you must implement some method that will send data to consumer. It achieved using ConnectionProvider attribute:

public class WPProvider : WebPart, IDataProvider
{
	[WebBrowsable(true), 
	WebDisplayName("Name"),
	Personalizable(PersonalizationScope.User)]
	public string SomeName { set; get; }

	protected override void CreateChildControls()
	{
		var lblName = new Label {Text = string.Format("Hi, my name is {0}", SomeName)};
		Controls.Add(lblName);
	}

	public string Name
	{
		get { return SomeName; }
	}

[ConnectionProvider("Data provider", "wpDataProvider", AllowsMultipleConnections = true)]
	public IDataProvider DataProviderConnectionPoint()
	{
		return this;
	}

My data provider web part implement IDataProvider interface, and have additional personalizable property SomeName.

Consumer web part must have a method that accept data and store it internally to process further. Method have attribute ConnectionConsumer:

public class WPConsumer : WebPart
{
	private IDataProvider _dataProvider;
	private Label lblGreetings;

	protected override void CreateChildControls()
	{
		lblGreetings = new Label();
		if(_dataProvider != null)
		{
			lblGreetings.Text = string.Format("Hi, {0}", _dataProvider.Name);
		}
		else
		{
			lblGreetings.Text = "No connection. :)";
		}
		Controls.Add(lblGreetings);
	}

	[ConnectionConsumer("Data provider", "wpDataConsumer", AllowsMultipleConnections = false)]
	public void DataConnectionPoint(IDataProvider provider)
	{
		_dataProvider = provider;
	}
}

You can see, that if web part isn’t connected, “No connection” message displayed, otherwise some greeting using provider data.  In what phase of web part life cycle you can successfully access data from provider? According this great post, event CreateChildControls() fire after connections was establish, that’s why you can use in in CreateChildControls(). Additional reference about connections you can find in msdn.

And the last question is how actually connecting web pats, when all code is ready. You have three ways: connect web part using SharePoint UI, connect web part declaratively (using WebPartConnection element) or through code in feature receiver (using SPConnectWebParts method).

Source code you can download here (14.89 kb)

Hope this helps.

Tags: ,

SharePoint | Web Parts

Pingbacks and trackbacks (1)+

Add comment

biuquote
  • Comment
  • Preview
Loading