Complete Code of SharePoint 2007 (MOSS/WSS) Connected Web Parts using IWebPartField Interface
Here is an example of connectable Webparts using IWebPartField interface. In the Provider Web Part, I have a drop down list box. This Web Part contains a list of categories from Northwind Database-Categories table. In the Consumer Web Part I have enlisted associated products from Products table.
Connection Provider Part:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
namespace WPConnectField
{
[Guid("391e3c0b-69b2-471c-ab47-0ca9aa1ecab1")]
public class WPConnectFieldProvider : System.Web.UI.WebControls.WebParts.WebPart, IWebPartField
{
System.Web.UI.WebControls.DropDownList CatList;
int CatListVal = 0;
public int CategoryID
{
get
{
return this.CatListVal;
}
}
[ConnectionProvider("Web part Connection Provider")]
public IWebPartField GetWPConnectFieldProvider()
{
return this;
}
public void GetFieldValue(FieldCallback callback)
{
callback.Invoke(this.CatListVal);
}
public PropertyDescriptor Schema
{
get
{
return TypeDescriptor.GetProperties(this)["Web part Connection Provider"];
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
CatList = new System.Web.UI.WebControls.DropDownList();
SqlConnection newSqlConnection = new SqlConnection();
SqlCommand newSqlCommand = new SqlCommand("Select CategoryID,CategoryName from categories", newSqlConnection);
newSqlCommand.CommandType = System.Data.CommandType.Text;
newSqlConnection.ConnectionString = "Integrated Security=True;Initial Catalog=Northwind;Data Source=pranab-sec";
newSqlConnection.Open();
CatList.DataValueField = "CategoryID";
CatList.DataTextField = "CategoryName";
CatList.AutoPostBack = true;
CatList.DataSource = newSqlCommand.ExecuteReader();
CatList.DataBind();
CatList.SelectedIndexChanged += new EventHandler(CatList_SelectedIndexChanged);
Controls.Add(CatList);
newSqlConnection.Close();
}
void CatList_SelectedIndexChanged(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
this.CatListVal = int.Parse(this.CatList.SelectedValue);
}
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
EnsureChildControls();
this.CatList.RenderControl(writer);
}
}
}
Connection Consumer Part:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Data;
using System.Data.SqlClient;
namespace WPConnectFieldConsumer
{
[Guid("864ce0eb-a0f1-4d15-8006-5afb21e78f36")]
public class WPConnectFieldConsumercs : System.Web.UI.WebControls.WebParts.WebPart
{
protected int selectedCatID;
protected System.Web.UI.WebControls.DataGrid NewDataGrid;
protected string SqlQuery = "Select ProductID,ProductName,UnitPrice from Products";
[ConnectionConsumer("Web Part Consumer")]
public void GetWPConnectedProviderInterface(IWebPartField connectProvider)
{
FieldCallback callback = new FieldCallback(ReceiveField);
connectProvider.GetFieldValue(callback);
}
public void ReceiveField(object objField)
{
if (objField != null)
{
this.selectedCatID = (int)objField;
if (this.selectedCatID != 0)
this.SqlQuery += " where CategoryID = " + this.selectedCatID.ToString();
}
}
protected override void OnPreRender(EventArgs e)
{
NewDataGrid = new System.Web.UI.WebControls.DataGrid();
SqlConnection newSqlConnection = new SqlConnection();
SqlCommand newSqlCommand = new SqlCommand(this.SqlQuery, newSqlConnection);
newSqlCommand.CommandType = System.Data.CommandType.Text;
newSqlConnection.ConnectionString = "Integrated Security=True;Initial Catalog=Northwind;Data Source=pranab-sec";
newSqlConnection.Open();
NewDataGrid.DataSource = newSqlCommand.ExecuteReader();
NewDataGrid.DataBind();
Controls.Add(NewDataGrid);
newSqlConnection.Close();
newSqlConnection.Dispose();
newSqlCommand.Dispose();
base.OnPreRender(e);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
}
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
EnsureChildControls();
this.NewDataGrid.RenderControl(writer);
}
}
}
Comments
Anonymous
August 27, 2007
PingBack from http://www.virtual-generations.com/2007/08/28/sharepoint-2007-link-love-08-28-2007/Anonymous
August 29, 2007
dear parnab; i want to create 4 webparts
- WEbpart A exposiing an integer
- Webpart B recieving integer from web part A
- Webpart C recieving integer from web part A elso exposing 2 integers
- WEbpart D reciving values from webpart B i have dont the above two points. useing IWebpartrowProvider interface. but when i deploys the webpart C all the connection options are disabled. Can u please explain. I am stuck here ssumair@gmail.com
Anonymous
September 03, 2007
Is this working? I have the same exact one and doesn't work for me. Apprecitae your reply. Thanks, ManjuAnonymous
September 03, 2007
Great piece of work.I had been struggling with this since a long time.However what i want exactly is a bit different. I would like to create 2 webparts( one displaying a drill-down report from SSRS and the other displaying a chart of the same data) and on click of a particular field in the report I want the chart to be displayed accordingly. It would be of great help if you could solve this problem. maildeepali1@gmail.comAnonymous
October 07, 2007
Is there a page creation and editing model in SharePoint WebServices that can be used to create and edit the content of a “publishing” page type? We can do it from the SharePoint SDK, but How would one go about this using the WebService model?Anonymous
December 26, 2007
I m not able to filter the second web part. it is not all coming to public void ReceiveField(object objField) { if (objField != null) { this.selectedCatID = (string)objField; if (this.selectedCatID != null) this.SqlQuery += " where cmp_countrynameid = " + this.selectedCatID.ToString(); } } method. I made a selection the DropDownList but the consumer web part is not consuming the selectedIndex id.Anonymous
January 20, 2008
Hi Rajesh, This method will be invoked in the Getfieldvalue method of the provider. Try to debug and see if that method is getting executed."connectProvider.GetFieldValue(callback);" is responsible for passing the valueAnonymous
January 20, 2008
How dow I do if I want to provide and consume multiple values, i.e. a drop down and a string?Anonymous
January 22, 2008
I copied the code and deployed the web part, but it's not working as expected, change the dropdownlist, nothing happenAnonymous
March 04, 2008
Excelent Usefull for me How can I send Qutions to U please give U r mail Id to meAnonymous
November 10, 2008
getfieldvalue is not getting invoked at all, nor consumer is notified. should i do any othher specific settings beyond the code for that to work? when i change value in combo, there isnt any call made to the consumer. should i do any other setting beyond the code here?Anonymous
August 04, 2009
Is there any samples out there that would allow connecting a IWebPartField to a standard sharepoint list or document library?Anonymous
July 08, 2010
Any idea for following post? Instead of Iwebpartfield - I am using IWebPartRow interface ! As per client's requirement, provider webpart renders dynamic fields on the page unlike creating them in the CreateChildControls() method. social.msdn.microsoft.com/.../5667a865-f9b0-430e-81d5-1a42acbe75d2