Overview
In SharePoint, a web part can be 'connected' to another web part to provide some information at run-time. The Provider web part pushes out the contracted information and the Consumer web part is set up to receive and consume the shared information.
Components of Connectable webparts
1) Interface: This custom interface will define the data to be shared from the Provider to the Consumer web part.
2) Provider web part: Implements a data interface - this interface is a definition of the data that will be shared from the Provider web part to the Consumer web part.
Provide a ConnectionProvider method which will expose the interfacing data to the Consumer web part.
3) Consumer web part: ConnectionConsumer method which will receive the interfacing data
Consume the interfacing data and provide enriching details to the user.
Steps for creating connectable web parts
1) Create an Empty SharePoint Project named ConnectedWebparts in Visual Studio 2010.
2) we add a new C# Interface code class and name it IUser.
3) Add a new web part named WPProvider in the project which will be the provider webpart. Make the changes as highlighted in the figure below.
4) Add a new web part named WPConsumer in the project which will be the provider webpart. Make the changes as highlighted in the figure below.
5) Build and Deploy the solution.
6) Add the Consumer webpart on the webpart page.
7) Add the Provider webpart on the webpart page.
8) Edit the Provider webpart and connect it to the consumer.
9) Now the 2 webparts have been connected to each other using the interface. In the provider webpart, type a username in the textbox and click "Send User". You can see the name is displayed in the consumer webpart as highlighted in the figure below.
Reference
http://www.c-sharpcorner.com/uploadfile/DipalChoksi/connectable-web-parts-in-sharepoint-2010/
http://www.dotnetcurry.com/ShowArticle.aspx?ID=678
Components of Connectable webparts
1) Interface: This custom interface will define the data to be shared from the Provider to the Consumer web part.
2) Provider web part: Implements a data interface - this interface is a definition of the data that will be shared from the Provider web part to the Consumer web part.
Provide a ConnectionProvider method which will expose the interfacing data to the Consumer web part.
3) Consumer web part: ConnectionConsumer method which will receive the interfacing data
Consume the interfacing data and provide enriching details to the user.
Steps for creating connectable web parts
1) Create an Empty SharePoint Project named ConnectedWebparts in Visual Studio 2010.
2) we add a new C# Interface code class and name it IUser.
Code Snippet
- namespace ConnectedWebparts
- {
- public interface IUser
- {
- string UserName { get; }
- }
- }
3) Add a new web part named WPProvider in the project which will be the provider webpart. Make the changes as highlighted in the figure below.
Code Snippet
- namespace ConnectedWebparts.WPProvider
- {
- [ToolboxItemAttribute(false)]
- public class WPProvider : WebPart,IUser
- {
- //From the UI, we collect the user selections
- Label lblUsername = null;
- TextBox txtUsername = null;
- Button btn = null;
- protected override void CreateChildControls()
- {
- lblUsername = new Label();
- txtUsername = new TextBox();
- btn = new Button();
- lblUsername.Text = "User name: ";
- btn.Text = "Send User";
- this.Controls.Add(lblUsername);
- this.Controls.Add(txtUsername);
- this.Controls.Add(btn);
- }
- public string UserName
- {
- get { return txtUsername.Text; }
- }
- [ConnectionProvider("UserName")]
- public IUser ConnectionProviderMethod()
- {
- // This method will expose the shared data to the Consumer.
- return this;
- }
- }
- }
4) Add a new web part named WPConsumer in the project which will be the provider webpart. Make the changes as highlighted in the figure below.
Code Snippet
- namespace ConnectedWebparts.WPConsumer
- {
- [ToolboxItemAttribute(false)]
- public class WPConsumer : WebPart
- {
- Label lblUser = null;
- IUser oIUser = null;
- //Receive the data from the Provider web part here
- [ConnectionConsumer("UserName")]
- public void ConsumerMethod(IUser user)
- {
- oIUser = user;
- }
- protected override void CreateChildControls()
- {
- // Here we utilize the data received from the Provider
- Label lblDisplayText = new Label();
- if (oIUser != null)
- {
- if (oIUser.UserName != "")
- lblDisplayText.Text = "Hello " + oIUser.UserName;
- else
- lblDisplayText.Text = "Hello Guest";
- }
- else
- lblDisplayText.Text = "No provider connected";
- this.Controls.Add(lblDisplayText);
- }
- }
- }
5) Build and Deploy the solution.
6) Add the Consumer webpart on the webpart page.
7) Add the Provider webpart on the webpart page.
8) Edit the Provider webpart and connect it to the consumer.
9) Now the 2 webparts have been connected to each other using the interface. In the provider webpart, type a username in the textbox and click "Send User". You can see the name is displayed in the consumer webpart as highlighted in the figure below.
Reference
http://www.c-sharpcorner.com/uploadfile/DipalChoksi/connectable-web-parts-in-sharepoint-2010/
http://www.dotnetcurry.com/ShowArticle.aspx?ID=678
No comments:
Post a Comment