Webpart connections are one of core fundamental of webpart infrastructure. Webpart connections allow end user to build rich and advance webpart.
Webpart connection depends on two things provider and consumer.A provider is one which gives information to consumer. A webpart can have any of these two roles or both.
The webpart framework allows two web parts to communicate and exchange data. This webpart connection helps to implement master/details or filter webpart. In this case one webpart act as a provider and other act as a consumer.
Provider can have any number of consumers. But consumer can have only one instance of provider.
Note: Earlier version of SharePoint supported cross page webpart connection, but in 2010 we will be able to connect the webpart in same page only.
Now let’s see how to create a connectable webpart.Connectable webpart can be created in 3 steps.
1. Create an Interface
2. Create a provider WebPart.
3. Create a consumer WebPart.
1. Create an Interface:
The provider and consumer webpart needs to communicate with each other with some data. For this purpose we implement a common interface which is responsible for exchanging information between a Web Part provider and Web Part consumer.
Steps:
1. Open Visual studio and select an empty SharePoint project as shown below.

2. Enter the URL of the local site where you want to deploy and select Deploy as farm solution since SharePoint connections not supported in sandbox solution.
3. After the project is created select the project and click on add->New Item->Interface as shown in the screenshot
4. The interface defines only the properties that correspond to the data shared between provider and consumer.
In this example I am using the PurchaseOrderID which will be shared between the provider and consumer webpart.
The interface should be implemented as public.The PurchaseOrderID property can be read only.
2. Create a provider webpart.
Create a webpart which provides the data to consumer webpart.
Steps 1: Right clicks the Project -> select Add new Item -> Select webpart and click ok.
2. The provider webpart should implement the interface (IPurchaseOrderID) which uses the Property “id” to provide a consumer with PurchaseOrderID.
3. The user controls for WebPart needs to be defined in CreateChildControls Method. In this example I am using grid view to display the PurchaseOrderID, Ship Date and Command field to select the particular PurchaseOrderID, so that the details about the Purchase Order will be displayed in another WebPart (Consumer WebPart).
4. We need to add the event handler to the grid view, so whenever the selectedindexchanged the PurchaseOrderID will be captured and sent to consumer WebPart. The implementation is shown below.
5. The below method which is used to get the data from database for binding the data with PurchaseOrderID grid view.
5. Next we need to create a method known as connection point. A connection point is a method that serves as an entry point into a connectable Web Part instance.
6. When we create a method it does not matter what we name the method. But it should return connection interface type and method should not have any parameters.
7. For SharePoint webpart manager to discover the connection point we need to add the “connectionprovider” attribute to the method as shown below. The method should always return this.
8 .We need to implement the interface. And just return the Id property.
3: Create a Consumer WebPart.
Steps:
1.To create a consumer webpart right click on the project->select add->New
Item->Select Webpart -> click Add
2.Next we need to define a local property (PurchaseOrderIDProvider) to store the reference of the connection interface. The instance of the interface is set in the
ProviderConnection Method.
3.The method for the consumer connection point takes a single parameter based on the interface type and has a void return type.
4.The method also requires the ConnectionConsumer attribute, which has two parameters, one to identify the WebPart and another one ”AllowMultipleConnections” this parameter will tell the WebPart whether it is allowed to connect to more than one connection at same time.
5.The connection between provider and consumer WebPart will happen just before OnPreRender Method.So all the databind should be done at OnPreRender Method.
6.The below method which is used to get the data from database for binding the data with PurchaseOrderDetails grid view. The PurchaseOrderID which is obtained from the provider is used to filter the Details.
Deploy the Solution by right clicking on the solution.
SharePoint site will open on the completion of Solution deployment.Now add the two web part to the page and click on the connections.










