Connecting a web part with a standard SharePoint list

October 9, 2007   11:22


Connecting a web part with a standard SharePoint listIt took me quite some time to figure out how it was possible to connect my web part with a standard SharePoint list. I’ve found a lot of examples with many, many lines of code, but at the end it turned out to be very simple.

You’ll only have to add 3 extra functions to your web-part to make it work. After adding those functions you are able to connect the web part with a standard list in SharePoint. The required functions are the GetRowData & SetConnectionInterface and you will have to override the OnPreRender.

Inside the GetRowData function you can write your personal code to read out the received row. The easiest way to figure out how to access the information from the returned rowData is to debug the code and then view the contents of the rowData object. In my example it turned out that the rowData is of the type DataRowView and that the document url can be extracted from the first position in the array.

The example below shows the required GetRowData interface functions, required to retrieve a document url from a document list.

//Required GetRowData interface
private IWebPartRow provider;
private void GetRowData(object rowData)
{
 if (rowData != null)
 {
  System.Data.DataRowView rowInfo = (System.Data.DataRowView) rowData;
  DocumentUrl = rowInfo.Row[0].ToString();
 }
}

//Required on PreRender interface
protected override void OnPreRender(EventArgs e)
{
 if (provider != null) provider.GetRowData(new RowCallback(GetRowData));
}

//Required connection interface
[ConnectionConsumer("Row")]
public void SetConnectionInterface(IWebPartRow provider)
{
 this.provider = provider;
}

Tags: , , , , , , , .





4 Comments »

  1. Genius! Works 100% better than the MSDN example, in that it actually works. Just the code I needed too.

    Blog subscribed to.

    Comment by Stephen Hynds — October 26, 2007 @ 12:33 pm

  2. Hi,
    This information is really helpful.
    I want to connect search Core results webpart to the Custom webpart.
    In the custom webpart I want to process the results and then display it in MOSS search center.

    Can we achieve this using Connectable webparts using IWebpartRow interface.

    I have tried the code posted, but I am getting the build error that RowCallBack method and Scema property are not implemented.

    I am strucked with this.

    Any help in this regard will be highly appreciated.

    Comment by mswin — November 27, 2007 @ 2:13 pm

  3. Hello,

    Thanks for that,
    Is it possible to directly put this code into the results.aspx files?
    If so, could you send me an exemple.
    I know that will be a little bit easy but could you send me the file modified.

    Thx in advance.

    Comment by Michel Schaffner — January 15, 2010 @ 10:49 am

  4. @Michel Hi. Unfortunately this is not possible. You have to add this code into your c# web part code.

    Comment by Edwin Vriethoff — January 18, 2010 @ 7:14 am

RSS feed for comments on this post. TrackBack URL

Leave a comment





The content expressed in this blog are those of Edwin Vriethoff and do not represent his employer's view in anyway. The contents of this blog has been carefully put together, but Edwin Vriethoff is not responsible in any way for any direct or indirect harm caused by individuals or organizations using the content of this blog in any way.