WebPart should be inherited from using Microsoft.SharePoint.WebPartPages.WebPart;
//--------------------------------------------------------------------------
// Code to be inserted in IDataProvider.cs
//--------------------------------------------------------------------------
namespace ConnectingWebPart
{
public interface IDataProvider
{
string ProposalID { get; }
}
}
//--------------------------------------------------------------------------
// Code to be inserted in Provider.cs
//--------------------------------------------------------------------------
namespace ConnectingWebPart
{
public class Provider : WebPart, IDataProvider
{
protected TextBox txtProvider;
protected Button btnSend;
protected override void CreateChildControls()
{
txtProvider= new TextBox();
btnSend= new Button();
btnSend.Text = "Send data";
Controls.Add(txtProvider);
Controls.Add(btnSend);
}
protected override void RenderContents(HtmlTextWriter writer)
{
txtProvider.RenderControl(writer);
btnSend.RenderControl(writer);
}
[System.Web.UI.WebControls.WebParts.ConnectionProvider("Proposal ID", AllowsMultipleConnections = true)]
public IDataProvider GetProposalProvider()
{
return this;
}
public string ProposalID
{
get
{
return txtProvider.Text;
}
}
}
}
//--------------------------------------------------------------------------
// Code to be inserted in Consumer.cs
//--------------------------------------------------------------------------
namespace ConnectingWebPart
{
public class Consumer: WebPart
{
protected TextBox txtProvider;
private IDataProvider ProposalProvider;
[System.Web.UI.WebControls.WebParts.ConnectionConsumer("Proposal ID")]
public void RegisterCustomerProvider(IDataProvider provider)
{
this.ProposalProvider = provider;
}
protected override void CreateChildControls()
{
txtConsumer= new TextBox();
Controls.Add(txtConsumer);
}
protected override void RenderContents(HtmlTextWriter writer)
{
if (this.ProposalProvider != null)
txtConsumer.Text = this.ProposalProvider.ProposalID;
txtConsumer.RenderControl(writer);
}
}
}
Deploying a Web Part assembly in SharePoint
After building a successful web part assembly it’s time to deploy it to SharePoint site, you can deploy a Web Part assembly to one of two locations:Bin directory & Global assembly cache.
I will deploy the web part assembly in the Global assembly cache.
Here are the steps:
1. Register the assembly using strong key name.
2. Add the DWP files to the project. Every Web Part class should have a corresponding .dwp file. This is an XML file containing some details of the Web Part, such as the title and the description, but more important, a link to the assembly containing the code that needs to be executed.
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>Provider Webpart</Title>
<Description></Description>
<Assembly> ConnectingWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=76367eea108dc205</Assembly>
<TypeName>ConnectingWebPart.Provider</TypeName>
</WebPart>
Same you can create for consumer web part too.
3. Your project should also contain one manifest.xml. This file is used during the deployment of the Web Part.
...
<SafeControls>
<SafeControl
Namespace="ConnectingWebPart"
TypeName="*"
/>
...
<DwpFiles>
<DwpFile FileName="Provider.dwp"/>
<DwpFile FileName="Consumer.dwp"/>
</DwpFiles>
4. Create a CAB file project and include the project output and the content files. Then includes the both the .dwp files through files option. Now build & copy the .CAB file this location “C:\program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin”.
5. Modify the web.config of your virtual server and elevate the trust-level to WSS-Medium.
<trust level="WSS_Medium" originUrl="" />
6. Use the STSADM.EXE tool to add the Web part package to the virtual server of your choice.
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o addwppack -filename ConnectingWebPartCab.CAB -url http://psspl-spserver:789 -globalinstall –force
If you make changes,first delete the package & then add the paakage again.
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o deletewppack -name ConnectingWebPartCab.CAB -url http://psspl-spserver:789
IISRESET.
7.Once the webparts are added to your site pages set the connection between the two webparts.