using wsswebparts = Microsoft.SharePoint.WebPartPages; using aspnetwebparts = System.Web.UI.WebControls.WebParts; using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls.WebParts; using System.Reflection; using System.IO; using Microsoft.SharePoint; using System.ComponentModel; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections.ObjectModel; using System.Collections; using System.Runtime.InteropServices; namespace CustomSmartToolsChart { [Guid("9e963b40-ede3-4db4-ba61-25af8b5caf4b")] public class CustomChart : System.Web.UI.WebControls.WebParts.WebPart { [WebBrowsable(true), Personalizable(true), Category("Chart Data"), WebDisplayName("List or Document Library"), WebDescription("The name of the list or document library to load data from.")] public string List { get; set; } [WebBrowsable(true), Personalizable(true), Category("Chart Data"), WebDisplayName("View"), WebDescription("The view of the selected list or document library to load data from (if empty, the default view will be used).")] public string View { get; set; } [WebBrowsable(true), Personalizable(true), Category("Chart Data"), WebDisplayName("Group By Field"), WebDescription("Column name of the list to use a group by field in the chart.")] public string GroupByField { get; set; } [WebBrowsable(true), Personalizable(true), Category("Chart Data"), WebDisplayName("Group By Sum Field"), WebDescription("Column name of the list to use as the sum of the group by field in the chart. If empty, the count function will be used instead.")] public string GroupBySumField { get; set; } [WebBrowsable(true), Personalizable(true), Category("Chart Size"), WebDisplayName("Width"), WebDescription("Width of the chart.")] public string ChartWidth { get; set; } [WebBrowsable(true), Personalizable(true), Category("Chart Size"), WebDisplayName("Height"), WebDescription("Height of the chart.")] public string ChartHeight { get; set; } [WebBrowsable(true), Personalizable(true), Category("Chart Template"), WebDisplayName("XAML Template"), WebDescription("XAML template to apply.")] public string XamlTemplate { get; set; } [WebBrowsable(true), Personalizable(true), Category("Chart Template"), WebDisplayName("DataPoint Template"), WebDescription("DataPoint template to apply.")] public string DataPointTemplate { get; set; } // ************** CONNECTION STUFF ******************// //Tell SharePoint the Web Part can accept connections List providers = new List(); [aspnetwebparts.ConnectionConsumer("Simple Consumer", "IFilterValues", AllowsMultipleConnections = true)] public void SetConnectionInterface(wsswebparts.IFilterValues provider) { this.providers.Add(provider); if (provider != null) { if (View != null && View != string.Empty) { List l = new List(); SPWeb contextWeb = SPContext.Current.Site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl); // Loop through all fields available in the specified View - We can filter on these foreach (string fieldString in contextWeb.Lists[List].Views[View].ViewFields.ToStringCollection()) { l.Add(new wsswebparts.ConsumerParameter(fieldString, wsswebparts.ConsumerParameterCapabilities.SupportsMultipleValues | wsswebparts.ConsumerParameterCapabilities.SupportsAllValue)); } provider.SetConsumerParameters(new ReadOnlyCollection(l)); } } } // ************** CONNECTION STUFF ******************// protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) { try { //this.EnsureChildControls(); // Gets any filter parameters if there are connections Filter Parameters bool validFilter = false; List filterValue = new List(); List filterField = new List(); foreach (wsswebparts.IFilterValues provider in this.providers) { if (provider != null) { string prop = provider.ParameterName; ReadOnlyCollection values = provider.ParameterValues; if (prop != null && values != null) { foreach (string v in values) { if (v != null && v.Length != 0) // Apply the filter { filterField.Add(provider.ParameterName); filterValue.Add("'" + v + "'"); validFilter = true; } } } } } SPList list = SPContext.Current.Web.Lists[this.List]; string internalGroupBy = list.Fields.GetField(this.GroupByField).InternalName; string internalGroupBySum = string.Empty; bool sumFieldUsed = false; if (this.GroupBySumField != null && this.GroupBySumField != string.Empty) { sumFieldUsed = true; internalGroupBySum = list.Fields.GetField(this.GroupBySumField).InternalName; } StringBuilder sb = new StringBuilder(); SPView viewToUse; if (this.View == null || this.View == string.Empty) viewToUse = list.DefaultView; else viewToUse = list.Views[this.View]; SPQuery q = new SPQuery(); q.Query = viewToUse.Query; DataTable dt = list.GetItems(q).GetDataTable(); if (dt != null) // Data available { // If there are filters via connections, remove entries not equal to the filter if (validFilter) { for(int i = 0; i < filterField.Count; i++) { string expression = filterField[i] + " <> " + filterValue[i]; DataRow[] results = dt.Select(expression); foreach (DataRow dr in results) { dr.Delete(); } } } if (sumFieldUsed) dt.Columns.Add(string.Format("{0}SmartToolsValue", internalGroupBySum), typeof(double), string.Format("CONVERT({0}, 'System.Double')", internalGroupBySum)); DataView dv = new DataView(dt); foreach (DataRow row in dv.ToTable(true, internalGroupBy).Rows) { dv.RowFilter = string.Format("[{1}]='{0}'", row[internalGroupBy].ToString(), internalGroupBy); string label = "[Empty]"; if (row[internalGroupBy] != null) if (row[internalGroupBy].ToString() != string.Empty) label = row[internalGroupBy].ToString(); if (sumFieldUsed) { string sumFieldValue = dt.Compute(string.Format("SUM({0}SmartToolsValue)", internalGroupBySum), dv.RowFilter).ToString(); sb.Append(string.Format(this.DataPointTemplate, label, sumFieldValue)); } else sb.Append(string.Format(this.DataPointTemplate, label, dv.Count.ToString())); } } string html = @"
"; if (XamlTemplate != null) { string xaml = this.ReplaceXamlTokens(this.XamlTemplate, sb.ToString()); string formattedHtml = string.Format(html, this.ID, this.ChartWidth, this.ChartHeight, xaml); writer.Write(formattedHtml); } } catch (Exception ex) { writer.Write(string.Format("{0}

Details: {1}", ex.Message, ex.ToString())); } } private string ReplaceXamlTokens(string xaml, string dataPoints) { return xaml.Replace("{DataPoints}", dataPoints).Replace("{ChartWidth}", this.ChartWidth).Replace("{ChartHeight}", this.ChartHeight); } public CustomChart() { this.ExportMode = WebPartExportMode.All; } } }