Tuesday 9 October 2012

SharePoint 2010 Chart WebPart from Custom Data Source


Steps
SharePoint chart WebPart can connect to another WebPart capable of providing data. The data provider WebPart should send data by data table format. In below example I have explained a data provider WebPart as the data source of out of the box chart WebPart.


1-Create a WebPart and name it as “ChartDataProvider”.
2- Code for “ChartDataProvider” WebPart.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Reflection;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace ChartFromExternalDataSource.ChartDataProvider
{
    [ToolboxItemAttribute(false)]
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class ChartDataProvider : WebPart, IWebPartTable
    {
        DataTable _table;
        protected override void CreateChildControls()
        {
        }

        public ChartDataProvider()
        {


            _table = new DataTable();

            DataColumn col = new DataColumn();
            col.DataType = typeof(string);
            col.ColumnName = "Item";
            _table.Columns.Add(col);

            col = new DataColumn();
            col.DataType = typeof(int);
            col.ColumnName = "NoOfUnits";
            _table.Columns.Add(col);

          

            DataRow row1 = _table.NewRow();
            row1["Item"] = "AC";
            row1["NoOfUnits"] = 2000;
            _table.Rows.Add(row1);

            DataRow row2 = _table.NewRow();
            row2["Item"] = "MusicSystem";
            row2["NoOfUnits"] = 5000;
            _table.Rows.Add(row2);

            DataRow row3 = _table.NewRow();
            row3["Item"] = "Washing Machine";
            row3["NoOfUnits"] = 1000;
            _table.Rows.Add(row3);

            DataRow row4 = _table.NewRow();
            row4["Item"] = "TV";
            row4["NoOfUnits"] = 4000;
            _table.Rows.Add(row4);
           
        }

        public PropertyDescriptorCollection Schema
        {
            get
            {
                return TypeDescriptor.GetProperties(_table.DefaultView[0]);
            }
        }

        public void GetTableData(TableCallback callback)
        {
            callback(_table.Rows);
        }

        public bool ConnectionPointEnabled
        {
            get
            {
                object o = ViewState["ConnectionPointEnabled"];
                return (o != null) ? (bool)o : true;
            }
            set
            {
                ViewState["ConnectionPointEnabled"] = value;
            }
        }

        [ConnectionProvider("Table", typeof(TableProviderConnectionPoint),
      AllowsMultipleConnections = true)]
        public IWebPartTable GetConnectionInterface()
        {
            return new ChartDataProvider();
        }

        public class TableProviderConnectionPoint : ProviderConnectionPoint
        {
            public TableProviderConnectionPoint(MethodInfo callbackMethod,Type interfaceType, Type controlType, string name, string id,
        bool allowsMultipleConnections) : base(callbackMethod, interfaceType, controlType, name, id,
                  allowsMultipleConnections)
            {
            }

            public override bool GetEnabled(Control control)
            {
                return ((ChartDataProvider)control).ConnectionPointEnabled;
            }

        }
    }
}




3-Deploy the web part to a SharePoint site.
4-In a WebPart page add a SharePoint Chart WebPart  and a “ChartDataProvider” WebPart.




5-Click Data&Appearance > Customize Your Chart then Select a Template.
6- Click Data&Appearance > Connect Chart To Data > Select “Connect to another Web Part”.




7-Select the “Chart Data Provider WebPart” from the drop down. 





2 comments:

  1. Hi Sudharshan,

    I did all the steps mentioned in the blog, but somehow the connections option in the edit menu is greyed out and disabled.

    Do you have any idea why that could be happening. I checked the Provider interface methods and things look fine there.

    ReplyDelete
  2. I figured that out. It seems Sandboxed solutions don't allow webpart connections. Making it farm based solution worked !!

    ReplyDelete