Connecting to Data / Connecting to a Web Part Table
In This Topic
    Connecting to a Web Part Table
    In This Topic

    It is sometimes required to develop a custom web part that can serve as data provider for Nevron Web Parts. We decided to base our implementation on the standard IWebPartTable interface. That said the Nevron Web Parts can extract data from any other web part that implements this interface.

    The Web Part Table connection panel is displayed when you select the Web Part Table option from the Data Source Type combo:

    The connection is defined by the following settings:

    Site Mode - lets you select the way in which the web part will connect to the site and web. The possible options are:

    Static Site - the site is specified explicitly from the Site setting. The web is specified relatively to the site.
    Current Site - only the site is obtained dynamically (the site is the current site in which the web part resides). The web is specified relatively to the site from the Web setting.
    Current Web - both the site and web are obtained dynamically (e.g. binds to the current web of the current site in which the web part resides).

    Site - only available in Static Site mode. The web part will automatically enumerate the currently available sites in all web applications for you. If you see the site in the combo just pick it - otherwise manually specify it in the site text box.

    Web - only available in Static Site and Current Site modes. For the site that you have chosen, the web part automatically enumerates the server relative paths of all webs beneath it. If you see the web just pick it - otherwise manually specify it in the web text box.

    Page - for the site and web that you have chosen, the web part will automatically enumerate all web pages on which you can possibly insert web part inside the selected web.

    Title - for the site-web-page that you have chosen the web part will enumerate the titles of the web parts that reside on this page and that implement the IWebPartTable interface.

    The Site and Web can be specified with GUIDs too.
    All properties of the Web Part Table connection can be specified with expressions too.
     Creating a Custom Web Part that implements the IWebPartTable interface
    The following content is intended for developers that want to create custom web parts that implement the IWebPartTable interface and thus surface data sources that cannot be accessed by the currently available data source connection methods. If you are not a developer or if you can access your data source with the currently available data connection methods - skip this section.

    Some sample code that outlines how to implement the IWebPartTable interface can be found at: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.iwebparttable.aspx. It is however worth mentioning that we think that this sample implementation is not correct, because of the following reasons:

    1. The Schema property getter of TableProviderWebPart returns a PropertyDescriptorCollection instance, that is constructed from the the first row of the DefaultView, while the GetTableData method returns the table data rows. You cannot access the values of the enumerated records with the returned property descriptors, because the property descriptors are for DataRowView instances, not for DataRow instances.

    2. The TableConsumer explicitly casts the enumerated data table objects to DataRow, which is the only reason for this example to work. If the returned ICollection for the data table is not a collection of DataRow items this sample will simply throw an exception.

    So following is some sample code that correctly implements the IWebPartTable interface.

    Data Provider Web Part
    Copy Code
      public class MyWebPartTableWebPart : Microsoft.SharePoint.WebPartPages.WebPart, IWebPartTable
      {
       ...
       DataTable m_DataTable = null;
       void InitDataTable()
       {
        if (m_DataTable != null)
         return;
        m_DataTable = new DataTable();
        
        m_DataTable.Columns.Add(new DataColumn("Company", typeof(String)));
        m_DataTable.Columns.Add(new DataColumn("Sales", typeof(Double)));
        
        m_DataTable.Rows.Add("Bigfoot", 124);
        m_DataTable.Rows.Add("Bigfoot", 234);
        m_DataTable.Rows.Add("Bigfoot", 782);
        m_DataTable.Rows.Add("Maison", 324);
        m_DataTable.Rows.Add("Maison", 534);
        m_DataTable.Rows.Add("Maison", 182);
        m_DataTable.Rows.Add("Leka", 524);
        m_DataTable.Rows.Add("Leka", 334);
        m_DataTable.Rows.Add("Leka", 282);
       }
       public void GetTableData(TableCallback callback)
       {
        InitDataTable();
        callback(m_DataTable.DefaultView);
       }
       public PropertyDescriptorCollection Schema
       {
        get
        {
         InitDataTable();
         return TypeDescriptor.GetProperties(m_DataTable.DefaultView[0]);<br>
        }
       }
       ...
     }
    

    The consumer of the web part data table on the other hand needs to only rely on the returned Schema and collection of objects as shown in the following code sample:

    Data Consumer Web Part
    Copy Code
      // somehow get a reference to the web part table interface
      IWebPartTable webPart = ...;
      // get the table schema
      PropertyDescriptorCollection tableSchema = webPart.Schema;
      // iterate through the data table row values
      webPart.GetTableData(delegate(ICollection tableData)
      {
       foreach (object row in tableData)
       {
        for (int col = 0; col < tableSchema.Count; col++)
        {
         // get the value for the current row/col
           object value = tableSchema[col].GetValue(row);
        }
       }
      });
    
    It is important to know that a webpart that implements the IWebPartTable needs to provide it's data synchronously, when it's GetTableData method is called. This is required, because Nevron WebParts will request the data from an AJAX handler, meaning that the page on which the data source webpart resides will not go through its full post-back lifecycle. That is why even though some SharePoint webparts implement the IWebPartTable interface, their implementation is mainly targeting collaboration via webparts connections (which is an old-fashioned post-back dependant method) and Nevron WebParts will not be able to consume their data.

    If the predefined set of available data connections is not fitting into your SharePoint application scenario you can contact Nevron Consulting at consulting@nevron.com for a price quote for a custom web part that connects to your data.