To write connectable web parts we need to do the following
First we need to define our own interface that will specify the data we want to pass from one web part to another.
The provider web part needs to do the following
- Implement the interface.
- Create a property which would be returning a reference to the interface.
- The property should be decorated with ConnectionProvider attribute.
The consumer web part needs to do the following
- It should contain the method which would receive the interface.
- The method should be decorated with ConnectionConsumer attribute.
Keeping the above information in mind let’s start
Create a new web part project within Visual Studio 2008. (HelloWorldConnectedWebPart)
Right click on the Project
Select Add New ItemàSharePointàWebPart.
Now rename the webpart1.cs and webpart2.cs class as HWProviderWebPart and HWConsumerWebPart respectively.
Now right click the project and add a new interface class.
public interface IStringData
{
string ProviderStringInfo { get; }
}
Our Provider class should implement this interface and define one property which would be returning the reference to the interface.
[Guid(“56978c39-1958-4128-a979-b9feaf2feb46”)]
public class HWProviderWebPart : WebPart, IStringData
{
public HWProviderWebPart()
{
}
protected override void CreateChildControls()
{
}
// the string info that would be passed to the consumer
protected string myInfo = “Hello World”;
// implement the property defined in the interface
public string ProviderStringInfo
{
get { return myInfo; }
}
// create a property which would be returning the interface reference
// decorate it with ConnectionProvider
[ConnectionProvider(“String Provider”)]
public IStringData ConnectionInterface()
{
return this;
}
}
Now let’s move to our Consumer Web Part.
[Guid(“3575c6de-e21a-4e5a-b7f0-fe1aa4844402”)]
public class HWConsumerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
public HWConsumerWebPart(){
}
protected override void CreateChildControls(){
}
IStringData myProviderInterface = null;
// The Consumer class should define a method that would accept
// the interface as an parameter
// Should be decorated with ConnectionConsumer attribute
[ConnectionConsumer(“String Provider”)]
public void GetInterface(IStringData providerInterface)
{
myProviderInterface = providerInterface;
}
protected override void Render(HtmlTextWriter writer)
{
try
{
// priting the value provided by provider web part
writer.Write(myProviderInterface.ProviderStringInfo);
}
catch(Exception ex)
{
writer.Write(“Error info “ + ex.Message);
}
}
}
Now build the project. ( Remove errors if any)
Right click the project.
Select Properties – Debug — Start browser with url ( Specify the site where the web part should be deployed)
Right click the project and select Deploy.
After Deploy Succeeds ,
Go to site actions — Site Settings — WebParts( Inside galleries) –Click on New– Select both the Provider and consumer web part — Populate Gallery.
Go to your home page — Edit page — Add both the web parts –Select the provider web part — Connections and Specify the connection.
That’s it..
