Wednesday, June 6, 2007

Communication between Web Parts

Following steps describes how to create two Web Parts that can communicate with each other:
1) Create a Contract create class.
ITextContract.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsumerProviderWebPart
{
public interface ITextContract
{
string CustomerName { get;set; }
}
}





2) Create a Provider class.

Provider.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ConsumerProviderWebPart
{
public class Provider : WebPart, ITextContract
{
TextBox tb = new TextBox();
Button btn = new Button();

protected override void CreateChildControls()
{
btn.Text = "Click Me";
btn.Click += new EventHandler(btn_Click);

tb.Text = "Avinash";
CustomerName = tb.Text;

this.Controls.Add(tb);
this.Controls.Add(btn);
}

protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
RenderChildren(writer);
}

void btn_Click(object sender, EventArgs e)
{
CustomerName = tb.Text;
}

[ConnectionProvider("Customer Provider")]
public ITextContract ProvideCustomerCommunicationPoint()
{
return this as ITextContract;
}

#region ITextContract Members

private string _CustomerName = "";

public string CustomerName
{
get
{
return _CustomerName;
}
set
{
_CustomerName = value;
}
}

#endregion
}
}




3) Create a Consumer class.

Consumer.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ConsumerProviderWebPart
{
public class Consumer : WebPart
{
private ITextContract providerWebPart;

[ConnectionConsumer("Customer Consumer")]
public void ReceiveCustomerName(ITextContract provider)
{
providerWebPart = provider;
}

protected override void Render(HtmlTextWriter writer)
{
if (providerWebPart != null)
writer.Write("Value provided is : " + providerWebPart.CustomerName);
base.Render(writer);
}
}
}




4) Strong-named the project.
5) Register the dll in GAC.
6) Make entry in your site web.config file.

web.config

<SafeControl Assembly="ConsumerProviderWebPart, Version=1.0.2713.19492, Culture=neutral, PublicKeyToken=f565ecbfa7f79652" Namespace="ConsumerProviderWebPart" TypeName="*" Safe="True" />



7) Create Provider.webpart file.

Provider.webpart


<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="ConsumerProviderWebPart.Provider" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">Provider</property>
<property name="Description" type="string">Provider Web Part.</property>
</properties>
</data>
</webPart>
</webParts>




8) Create Cosumer.webpart file.
Consumer.webpart


<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="ConsumerProviderWebPart.Consumer" />
<importErrorMessage>
Cannot import this Web Part.
</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">Consumer</property>
<property name="Description" type="string">Consumer Web Part.</property>
</properties>
</data>
</webPart>
</webParts>





9) Add both Provider and Consumer web parts in your site.
10) Establish connections between Provider and Consumer web part as shown in following snapshots:

Provider connection:



Consumer connection:



11) Test communication between two web parts by writing some text in Provider’s text box. Then press Provider’s button.



12) Typed text gets displayed in Consumer web part.



Suggested Links:
1) Custom Web Part Connections in SharePoint 2007
http://ablog.apress.com/?p=1300

2 comments:

zaffy said...

good article..i hope u can share many info n tutorial about sharepoint and web part...ur article help me a lots...thank you vary muchhh...

HP Laptop Part said...

Interesting stuff I must admit. But what I really want to know is how did you really compose all this coding?? Okay, once I get this down hopefully it will work for me because I tried a couple different ways and none of them seemed to work. No worries though, that's why Google is here! haha I can literally find anything to everything on the web nowadays but enough of that.. But then thing is, that whenever I try inserting something like this it say, "The project type is not supported by this installation" How can I solve this problem?

Google