|
Communicating web parts by code is performed by using the Provider-Consumer pattern. In this pattern one web parts acts as a provider exposing a set of public members (properties and methods) to be accessed by one or more consumers. The pattern exposes public members set by implementing an interface (we are going to called this interface as “communication interface”) To complete the communication process you must instruct SharePoint about how to connect the provider and consumer web parts by creating what we called “connection points”. We are going to discuss about this later.
In order to explain this web parts communication process let’s suppose you need to pass a list of strings from one web part to another (I’m going to assume some basic knowledge about programming web parts for SharePoint).
The first step for communicating the web parts is defining the “communication interface”:
public interface IStaffDotNetCommunicationContract
{
List<string> SelectedData { get; set; }
}
We are exposing here a property called SelectedData which returns a list of strings (this method is going to be called by the Consumer Web Part in order to get that list)
Once you have defined the interface you must be sure that you implement the interface at the Provider Web Part (as shown below):
public class StaffDotNetProviderWebPart : System.Web.UI.WebControls.WebParts.WebPart, IStaffDotNetCommunicationContract
{
//All the Web Part's Code goes here
}
After that, you have to set the Connection Point to the Provider Web Part. This connection point is a method which is going to be use by SharePoint to automatically connect this webpart with its consumers. This method must return an object whose type is the communication interface created previously. You must annotate the method with the ConnectionProvider attribute in order for SharePoint to know that this is the method to invoke to connect this web part with its consumers:
[ConnectionProvider("Name", "ID")]
public IStaffDotNetCommunicationContract StaffDotNetCommunicationPoint()
{
return this as IStaffDotNetCommunicationContract;
}
Now you have to create the Connection Point for the Consumer Web Part. In order to do that you have to define a member at the consumer web part whose type is the communication interface and you have to create a method annotated with the ConnectionConsumer attribute (this method is going to be used by SharePoint to set a reference to the provider web part from the consumer web part):
IStaffDotNetCommunicationContract webPartProvider;
[ConnectionConsumer("Name", "ID")]
public void ReceiveProviderData(IStaffDotNetCommunicationContract provider)
{
webPartProvider = provider;
}
Once this is done, you can access the data exposed by the provider from the consumer by calling the appropriated communication interface member:

Deploying the post sample
Code snippets at this post have been taken from the sample accompanying this post.
Again, I’m going to assume some basic knowledge about deploying SharePoint solutions.
Then, once you build and deploy the solution and once you have added the two web parts including in the sample to a new page in a SharePoint web site, you can connect the Provider Web Part to the Consumer Web Part by following the navigation path shown at the following picture:

To verify if everything works fine, publish your page, select some items of the Provider Web Part and then click “Send Data”. The Consumer Web Part will display the selected items from the Provider Web Part.
And that’s It. It's a simple and quick way to communicate your web parts.
Here’s the code with the example shown in this post.
See you soon!
|