This article will guide you through how to create a connection between Web Parts. You can create connections between Web Parts so that when you want to perform an action in one Web Part, it changes the contents of another Web Part.
How can I create connections between Web Parts in SharePoint 2010?
The first things you should keep in mind when creating a connectable Web Part are:
- Create an interface for data to be shared between Web Parts.
- Create a provider Web Part (a method with ConnectionProvider attributes).
- Create a consumer Web Part (a method with ConnectionConsumer attributes).
What follows are specific steps to create a connection between Web Parts, including code samples.
I. Creating an interface
This step is to create a class that implements all of the necessary methods and events for a connection interface.
1. In Solution Explorer, click the ConnectableWebParts project.
2. On the Project menu, click Add New Item.
3. In the Installed Templates pane, click Code and then create an Interface named ITextBoxString class. Make sure that the class is public:
II. Creating a provider Web Part
This step is to demonstrates how to create a Web Part that provides a string for other Web Parts to consume.
1. In the Solution Explorer, create Connectable Web Part name StringProvider, expanding SharePoint node, and then click 2010 in the Installed Templates pane.
2. In the StringProvider.cs file, create a StringProvider class that implements the ITextBoxString interface:
3. Then create a text box, a string, and a button:
4. Create properties of the ITextBoxString interface:
5. In the CreateChildControls method, clear all the controls from the Web Part, and then add the new control to the Web Part as follows:
6. Create an event handler for the button that you added in the previous step:
7. Create a method with ConnectionProvider atributes to return the ITextBoxString object to share with other Web Parts:
III. Creating a consumer Web Part
This step is to demonstrates how to create a Web Part that consumes and displays a string from a provider Web Part.
1. Create Connectable Web Parts named StringConsumer right-clicking on the Project menu, then click Add New Item. In the Installed Templates pane, expand the SharePoint node, and then click 2010.
2. Open the StringConsumer.cs file, create objects for an ITextBoxString provider and a text box:
3. In the CreateChildControls method, add the label to the Web Part. The default text is set to indicate if the consumer Web Part is connected to the provider Web Part:
4. In the OnPreRender method, add the following code to change the text of the label to the string from the StringProvider class:
5. Create a method with ConnectionConsumer attribute to assign the ITextBoxString object from the provider Web Part to the private ITextBoxString object in this consumer Web Part:
IV. Testing the Web Part connection
This step is to connect the provider and consumer Web Parts in your SharePoint site.
1. Open your SharePoint site.
2. Add the two Web Parts, StringProvider and StringConsumer, to your Web Part Page.
3. Click on the StringConsumer Web Part menu and point to Connections, then point to Get String Consumer From, and then choose String Provider.
4. In the StringProvider Web Part, type something in the text box, and click on Change Text button. Whatever string you type in the StringProvider will appear in the StringConsumer.
Source code is available upon request.
- Walkthrough: Creating Connectable Web Parts in SharePoint Foundation [MSDN]
- (Video demonstration) Creating Connectable Web Parts in SharePoint Foundation [MSN]