![]() |
Enterprise Systems »
SharePoint Server »
Web Parts
Beginner
License: The Code Project Open License (CPOL)
Connecting Custom Web Parts in SharePointBy Nioosha KashaniDescribes how to connect two SharePoint custom Web Parts using the Visual Studio Extension for Windows SharePoint Services. |
C# 1.0, C# 2.0, C# 3.0, .NET, Office, ASP.NET, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

SharePoint is a rich and powerful platform for both users and developers. We can customize and enrich SharePoint in many ways. One of the common ways is creating custom Web Parts. With a custom Web Part, we can implement our features quick and clean. One of the useful features of MOSS Web Parts is the ability to send and receive parameters. 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.
The Web Part created in this article will use Windows SharePoint Services 3.0. It will also work with MOSS 2007. To create the Web Part, we will be using Visual Studio 2005 installed on Windows Server 2003 with Extension for Windows SharePoint Services version 1.1 (you can also use Visual Studio 2008 with the Extension of WSS version 1.2). WSS or MOSS is also installed. You can use Microsoft Virtual PC for setting up such an environment in your development machine.

public class ProviderWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txt;
Button btn;
public ProviderWebPart()
{
this.Title = "Provider WebPart";
this.ExportMode = WebPartExportMode.All;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Table tbl;
TableRow row;
TableCell cell;
// A table for layout
tbl = new Table();
row = new TableRow();
cell = new TableCell();
// first row for title
cell.ColumnSpan = 2;
cell.VerticalAlign = VerticalAlign.Middle;
cell.HorizontalAlign = HorizontalAlign.Center;
Label lblTitle = new Label();
lblTitle.Text = "This WebPart will send a parameter to a consumer:";
cell.Controls.Add(lblTitle);
row.Controls.Add(cell);
tbl.Controls.Add(row);
// second row of table for textbox and button
row = new TableRow();
cell = new TableCell();
cell.VerticalAlign = VerticalAlign.Middle;
cell.HorizontalAlign = HorizontalAlign.Center;
txt = new TextBox();
txt.Text = "";
txt.Width = Unit.Pixel(120); ;
cell.Controls.Add(txt);
row.Controls.Add(cell);
cell = new TableCell();
cell.VerticalAlign = VerticalAlign.Middle;
cell.HorizontalAlign = HorizontalAlign.Center;
btn = new Button();
btn.Text = "Send...";
btn.Click += new EventHandler(btn_Click);
cell.Controls.Add(btn);
row.Controls.Add(cell);
tbl.Controls.Add(row);
//add table to webpart
this.Controls.Add(tbl);
}
void btn_Click(object sender, EventArgs e)
{
// will be added later
}
}
Note: The above code will render the interface of the ProviderWebPart.
namespace CommunicationInterface
{
public interface ICommunicationInterface
{
string Parameter1 { get; }
}
}
As you can see, we declared an interface for communication with one parameter. You can extend it and add your own parameters.

Use CommunicationInterface;
and change the class declaration to:
public class ProviderWebPart :
System.Web.UI.WebControls.WebParts.WebPart,
ICommunicationInterface
Parameter1 property in the Web Part class using the following code:// implement the Parameter1 property from interface
protected string _parameter1 = "";
public string Parameter1
{
get { return _parameter1; }
}
ConnectionProvider attribute.
The parameters of ConnectionProvider are the display name and the real name (ID) of the connection. When we declare more than one connection provider
and consumer (like SharePoint Web Parts), we must choose a unique name for IDs of connections in a Web Part.// create a property that return the interface reference
// and decorate it with ConnectionProvider
[ConnectionProvider("Parameter1 Provider",
"Parameter1 Provider")]
public ICommunicationInterface ConnectionInterface()
{
return this;
}
btn_click event:// set connection provider property with required textbox info.
this.localParameter1 = txt.Text;
Now, the whole code of ProviderWebPart must be like this:
public class ProviderWebPart :
System.Web.UI.WebControls.WebParts.WebPart,
ICommunicationInterface
{
TextBox txt;
Button btn;
// implement the Parameter1 property from interface
protected string _parameter1 = "";
public string Parameter1
{
get { return _parameter1; }
}
// create a property that return the interface reference
// and decorate it with ConnectionProvider
[ConnectionProvider("Parameter1 Provider",
"Parameter1 Provider")]
public ICommunicationInterface ConnectionInterface()
{
return this;
}
public ProviderWebPart()
{
this.Title = "Provider WebPart";
this.ExportMode = WebPartExportMode.All;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Table tbl;
TableRow row;
TableCell cell;
// A table for layout
tbl = new Table();
row = new TableRow();
cell = new TableCell();
// first row for title
cell.ColumnSpan = 2;
cell.VerticalAlign = VerticalAlign.Middle;
cell.HorizontalAlign = HorizontalAlign.Center;
Label lblTitle = new Label();
lblTitle.Text = "This WebPart will send a parameter to a consumer:";
cell.Controls.Add(lblTitle);
row.Controls.Add(cell);
tbl.Controls.Add(row);
// second row of table for textbox and button
row = new TableRow();
cell = new TableCell();
cell.VerticalAlign = VerticalAlign.Middle;
cell.HorizontalAlign = HorizontalAlign.Center;
txt = new TextBox();
txt.Text = "";
txt.Width = Unit.Pixel(120); ;
cell.Controls.Add(txt);
row.Controls.Add(cell);
cell = new TableCell();
cell.VerticalAlign = VerticalAlign.Middle;
cell.HorizontalAlign = HorizontalAlign.Center;
btn = new Button();
btn.Text = "Send...";
btn.Click += new EventHandler(btn_Click);
cell.Controls.Add(btn);
row.Controls.Add(cell);
tbl.Controls.Add(row);
//add table to webpart
this.Controls.Add(tbl);
}
void btn_Click(object sender, EventArgs e)
{
// set connection provider property with required textbox info.
this._parameter1 = txt.Text;
}
}
[assembly: CLSCompliant(true)]
to:
[assembly: CLSCompliant(false)]

Now, ProviderWebPart is ready to use in SharePoint and we must create a consumer Web Part.
[assembly: CLSCompliant(true)]
to:
[assembly: CLSCompliant(false)]
using CommunicationInterface;
ConsumerWebPart class, add the following code:public class ConsumerWebPart :
System.Web.UI.WebControls.WebParts.WebPart
{
Label lblTitle;
Label lblResult;
///// the string info consumer from custom 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("Parameter1 Consumer",
"Parameter1 Consumer")]
public void GetConnectionInterface(ICommunicationInterface
_connectionInterface)
{
connectionInterface = _connectionInterface;
}
/////////////////////////////////////////////////////////
public ConsumerWebPart()
{
this.Title = "Consumer WebPart";
this.ExportMode = WebPartExportMode.All;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Table tbl;
TableRow row;
TableCell cell;
// A table for layout
tbl = new Table();
row = new TableRow();
cell = new TableCell();
// first row for title
cell.VerticalAlign = VerticalAlign.Middle;
cell.HorizontalAlign = HorizontalAlign.Center;
lblTitle = new Label();
lblTitle.Text = "This WebPart will recieve " +
"a parameter from a provider:";
cell.Controls.Add(lblTitle);
row.Controls.Add(cell);
tbl.Controls.Add(row);
//second row for result
row = new TableRow();
cell = new TableCell();
cell.VerticalAlign = VerticalAlign.Middle;
cell.HorizontalAlign = HorizontalAlign.Center;
lblResult = new Label();
//check the connectionInterface for recieving the parameter1
if (connectionInterface != null)
{
lblResult.Text = connectionInterface.Parameter1+
" is recieved!";
}
else
{
lblResult.Text = "nothing is recieved!";
}
cell.Controls.Add(lblResult);
row.Controls.Add(cell);
tbl.Controls.Add(row);
//add table to webpart
this.Controls.Add(tbl);
}
}

After creating and deploying our Web Parts, we can check the functionality in the SharePoint environment.


The presented sample was simple. You can define your parameters in CommunicationInterface and transfer them between Web Parts. Each Web Part can be a consumer and provider at the same time.
The sample code showed that we can connect our custom Web Parts just like Microsoft SharePoint Server Web Parts. So, we can develop more advanced and generic connected Web Parts.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 Jun 2009 Editor: Smitha Vijayan |
Copyright 2009 by Nioosha Kashani Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |