SharePoint 2010 Cookbook: Creating Connections Between Web Parts

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:

public interface ITextBoxString


{


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, ITextBoxString


3. 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:


All SharePoint Versions

The web parts are functional components that extend your SharePoint environment whether it’s hosted, on-premises, or part of Microsoft® Office 365.

SharePoint 2013, 2016, 2019, Online (Office 365)

On-Premises Only

These web parts extend SharePoint beyond its out-of-the-box capabilities by tailoring it to your requirements with Bamboo Solution’s growing portfolio of SharePoint Web Parts.

SharePoint 2013, 2016, 2019

SharePoint

Product Suites

Experience greater power and savings by bundling our SharePoint apps and web parts.


Essentials Suite


Essentials Plus Suite


Bamboo Premier Suite


Project Management Suite


Knowledge Management Suite


External User Manager


 

For more information on our product suites, contact us.

Featured Services

SharePoint Health Check

A SharePoint Health Check will identify the causes of issues and risks associated with your specific environment, and is custom tailored to provide you with the best recommendations to optimize your SharePoint environment.

SQL Health Check

Document recommendations relating to performance, stability, availability, or a specific focus you request of your SQL Server database instances.

My SharePointXperts

The truth is that each SharePoint skill may not be a full time job for many organizations, and it is nearly impossible for one person to do everything you need – so augment your team with SharePointXperts; providing the skill sets you need when you need them!