All of us is now familiar with Web Part development, we can implement our features quick and clean. Today I would like to introduce to you one of the useful features of MOSS Web Parts is the ability to send and receive parameters between Web Parts in same page. We can easily filter a data view Web Part according to the results of another Web Part. It’s good to know that we can connect our custom Web Parts as well.

So all the things that we need:
– Create Provider WebPart which will used to send parameters to another WebParts
– Create Consumer WebPart which will be received parameters from Provider WebPart
– Setup ConnectionInterface for this communication.
– Deploy solution to SharePoint server

In this post, I will use Visual Studio 2008 run on 64x system so there’re required for Visual Studio 2008 extensions for Windows SharePoint Services 3.0

OK, Let start with

Create Provider WebPart

1. After install WSS3.0 Viusal studio 2008 extensions, in New Project dialog will appear new choice for SharePoint. Now you create a Web Part project as below and named Provider, set Solution Name: WebPartConnection

2. Select Full Trust in Select Trust Level dialog

3. Delete WebPart1 folder and then select Add New Item as below. Named as Provider

4. Add following code to Provider.cs

Label lblTitle;
TextBox txtParameter1;
TextBox txtParameter2;
Button btnSend;public Provider()
{
}protected override void CreateChildControls()
{
base.CreateChildControls();
lblTitle = new Label();
lblTitle.Text = “Type for your messages”;

txtParameter1 = new TextBox();
txtParameter2 = new TextBox();

btnSend = new Button();
btnSend.Text = “Send Messages”;
btnSend.Click += new EventHandler(btnSend_Click);

this.Controls.Add(lblTitle);
this.Controls.Add(txtParameter1);
this.Controls.Add(txtParameter2);
this.Controls.Add(btnSend);
}

void btnSend_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}

Create Consumer WebParts
In this post, I wil show you a demo for provider send 2 messages parameters to consumer.

1. Same to Provider project, Right click on solution, choose Add New Project and then select WebPart project under SharePoint category. Name it Consumer

2. Delete WebPart1 folder and add new WebPart item. Named as Consumer

3. Double click on Consumer.cs and add following code

public class Consumer : System.Web.UI.WebControls.WebParts.WebPart
{
Label lblTitle1;Label lblMessage1;Label lblTitle2;

Label lblMessage2;
public Consumer()

{

}
protected override void CreateChildControls()

{

base.CreateChildControls();
//initialize controls

lblTitle1 = new Label();

lblTitle1.Text = “Your Parameter 1: “;
lblTitle2 = new Label();

lblTitle2.Text = “<br/>Your Parameter2: “;
lblMessage1 = new Label();

lblMessage1.Text = “no message”;
lblMessage2 = new Label();

lblMessage2.Text = “no message”;

//add table to webpart

this.Controls.Add(lblTitle1);

this.Controls.Add(lblMessage1);

this.Controls.Add(lblTitle2);

this.Controls.Add(lblMessage2);

}

}

Setup Connection Interface
Finally we need setup connection interface and declare thi interface for commnunicaiton betweeb two WebParts

1. Right click on solution, choose Add New Project and then select Class Library. Name it CommunicationInterface.

2. Right click on CommunicationInterface and choose properties. In Signing tab, check “Sign the Assembly”, choose New from drop down list and then input key name file as interface and uncheck Protect key file with password.

3. Rename Class1.cs to ICommunicationInterface.cs and replace the following code with declare 2 Parameter as below

namespace CommunicationInterface
{
public interface ICommunicationInterface
{
string Parameter1 { get; }
string Parameter2 { get; }
}}

4. Right click and Build the CommunicationInterface project

5. Come back to Provider project, choose Add reference, then choose the Projects tab and select the CommunicationInterface project.

6. Now, we will implement the interface in Provider Web Part class. Open Provider.cs, add using CommunicatoinInterface
Change class decleration

public class Provider : System.Web.UI.WebControls.WebParts.WebPart, ICommunicationInterface

7. Add folling code to the class:

// implement the Parameter1 property from interface
protected string _parameter1 = “”;
public string Parameter1
{
get { return _parameter1; }
}
// implement the Parameter2 property from interface
protected string _parameter2 = “”;
public string Parameter2
{
get { return _parameter2; }
}// create a property that return the interface reference
// and decorate it with ConnectionProvider
[ConnectionProvider(“Parameters Provider”,”Parameters Provider”)]
public ICommunicationInterface ConnectionInterface()
{
return this;
}

8. Implement for btnSend_Click

void btnSend_Click(object sender, EventArgs e)
{
this._parameter1 = txtParameter1.Text;
this._parameter2 = txtParameter2.Text;
}

9. Open properties folder, open AssemblyInfo.cs and change [assembly: CLSCompliant(true)] to [assembly: CLSCompliant(false)]

10. Build project

11. Come to Consumer project, choose Add reference, then choose the Projects tab and select the CommunicationInterface project

12. Open Consumer.cs, then add using CommunicationInterface; and add folling code below

///// the string info consumer from reciever
ICommunicationInterface connectionInterface = null;
// The consumer webpart  must define a method that
// would accept the interface as an parameter
// and must be decorated with ConnectionConsumer attribute
[ConnectionConsumer(“Parameters Consumer”,”Parameters Consumer”)]
public void GetConnectionInterface(ICommunicationInterface _connectionInterface)
{
connectionInterface = _connectionInterface;
}

13. Under CreateChildControl method, check ConnectionInterface and display parameters value as following code

protected override void CreateChildControls()
{
base.CreateChildControls();
//initialize controlslblTitle1 = new Label();lblTitle1.Text = “Your Parameter 1: “;
lblTitle2 = new Label();

lblTitle2.Text = “<br/>Your Parameter2: “;
lblMessage1 = new Label();

lblMessage1.Text = “no message”;
lblMessage2 = new Label();

lblMessage2.Text = “no message”;
//check the connectionInterface and display parameters

if (connectionInterface != null)

{

lblMessage1.Text = connectionInterface.Parameter1;

lblMessage2.Text = connectionInterface.Parameter2;

}
//add table to webpart

this.Controls.Add(lblTitle1);

this.Controls.Add(lblMessage1);

this.Controls.Add(lblTitle2);

this.Controls.Add(lblMessage2);

}

14. In the properties folder, double click on AssemblyInfo.cs and change
[assembly: CLSCompliant(true)] to [assembly: CLSCompliant(false)]

15. Build Consumer Project

Deploy Solution to SharePoint server

1. Rebuild CommunicationInterface project, open C:\Windows\assembly, then drag and drop CommunicationInterface.dll to this folder

2. On Provider project, rightclick on project and select Properties, select Debug Tab, fill your SharePoint site url you want to deploy to Start browser with URL textbox.

3. Rebuild Provider project, then select Deploy to deploy Provider webpart

4. On Consumer project, rightclick on project and select Properties, select Debug Tab, fill your SharePoint site url you want to deploy to Start browser with URL textbox.

5. Rebuild Consumer project, then select Deploy to deploy Provider webpart

6. Add 2 webpart Provider and Consumer to page

7. From Consumer webpart, select edit, then select Provider Webpart connection as below

8. Click Public on the page

9. Check functionality of our WebPart by type some work and click Send Messages.

Hoang Nhut Nguyen
Email: nhutcmos@gmail.com
Skype: hoangnhut.nguyen