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);

}

}

}