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.
Challenge:
How can I create connections between Web Parts in SharePoint 2010?
Solution:
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:
{
string TextBoxString { get; set; }
}
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:
public class StringProvider : WebPart, ITextBoxString3. Then create a text box, a string, and a button:
private TextBox myTextBox;private String _textBoxString = String.Empty;
private Button myButton;
4. Create properties of the ITextBoxString interface:
[Personalizable()]public string TextBoxString
{
get
{
return _textBoxString;
}
set
{
_textBoxString = value;
}
}
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:
protected override void CreateChildControls(){
Controls.Clear();
myTextBox = new TextBox();
Controls.Add(myTextBox);
myButton = new Button();
myButton.Text = “Change Text”;
Controls.Add(myButton);
myButton.Click += new EventHandler(myButton_Click);
}
6. Create an event handler for the button that you added in the previous step:
void myButton_Click(object sender, EventArgs e){
if (myTextBox.Text != String.Empty)
{
TextBoxString = myTextBox.Text;
myTextBox.Text = String.Empty;
}
}
7. Create a method with ConnectionProvider atributes to return the ITextBoxString object to share with other Web Parts:
[ConnectionProvider(“Provider for String From TextBox”, “TextBoxStringProvider”)]public ITextBoxString TextBoxStringProvider()
{
return this;
}
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:
private ITextBoxString _myProvider;private Label myLabel;
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:
protected override void CreateChildControls(){
Controls.Clear();
myLabel = new Label();
myLabel.Text = “Default text”;
Controls.Add(myLabel);
}
4. In the OnPreRender method, add the following code to change the text of the label to the string from the StringProvider class:
protected override void OnPreRender(EventArgs e){
EnsureChildControls();
if (_myProvider != null)
{
myLabel.Text = _myProvider.TextBoxString;
}
}
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:
[ConnectionConsumer(“String Consumer”, “StringConsumer”)]public void TextBoxStringConsumer(ITextBoxString Provider)
{
_myProvider = Provider;
}
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.
Notes:
Source code is available upon request.
See Also:
- Walkthrough: Creating Connectable Web Parts in SharePoint Foundation [MSDN]
- (Video demonstration) Creating Connectable Web Parts in SharePoint Foundation [MSN]