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


hi, can you suggest me how to connect two webParts programmatically?
I get this error: Error info Object reference not set to an instance of an object
for me it is not working. i am getting error messgae
Error 1 Inconsistent accessibility: parameter type ‘Connwebpartsample.IStringData’ is less accessible than method ‘Connwebpartsample.Consumerhelloworldwebpart.Consumerhelloworldwebpart.GetInterface(Connwebpartsample.IStringData)’
i am getting below error message
Inconsistent accessibility: return type ‘connectablewebpart.IUser’ is less accessible than method ‘connectablewebpart.ProviderWP.cs.ProviderWP.ConnProviderMethod()’.
please tell me how to resolve. i am working on sharepoint2010.
[...] http://nishantrana.wordpress.com/2009/02/14/creating-a-hello-world-connectable-web-parts-in-sharepoi… LikeBe the first to like this post. [...]
Getting error
‘HelloWorldConnectedWebPart.HWProviderWebPart’ does not implement interface member ‘HelloWorldConnectedWebPart.IStringData.ProviderStringInfo’
‘HelloWorldConnectedWebPart.HWProviderWebPart’ does not implement interface member ‘HelloWorldConnectedWebPart.IStringData.ProviderStringInfo’
Nishant,
Any idea on below post ?
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5667a865-f9b0-430e-81d5-1a42acbe75d2/#c7b4324d-a8b1-48b0-a8ec-bd174ff06bf3
I can deploy the solution but get and an error “Object Refrence not set to an instance of the Object” when i try to add these webparts on the webpage.
It works.
excellent.
simple and brief , wonderful article to begin with connected webparts.
Nice – but how to consume data (in my custom webpart) from the out-of-the-box dataform web part.
The kind of connection where you click a cell value (link) and the consumer web part shows items filteret by the recieved value ?
Excellent…..worked like majic…..
I’m having problem on the consumer…
-when deployed and refresh the page my consumer got this error
“Error info The method or operation is not implemented”
Please help, I received this error when building
Error on HWProviderWebPart
[ConnectionProvider("String Provider")]
public IStringData ConnectionInterface()
{
return this;
}
Error 1 Cannot implicitly convert type ‘HelloWorldConnectedWebPart.HWProviderWebPart’ to ‘HelloWorldConnectedWebPart.IStringData’. An explicit conversion exists (are you missing a cast?)
I had the same issue and didn’t implement the IStringData interface in the HWProviderWebPart class.
Make sure you implement the IStringData interface with the HWProviderWebPart:
public class HWProviderWebPart : System.Web.UI.WebControls.WebParts.WebPart, IStringData
I think you forgot to inherit the Interface IStringData.. Please make sure that one..
[...] Creating a Hello World Connectable Web Parts in SharePoint [...]