19 04 2013
SharePoint WebPart with Definable or Dynamic Filter Connections
Here’s a code sample which will give you a web part that will have a property (accessible via the Toolpane) that lets you define a semi-colon separated list of the names of Filter Connection points. You can then use any standard SharePoint (or even PerformancePoint) filters connected to the web part and use them how you wish in the code.
Note that I’m building the connection points via a Property, but you can do this any way you wish at runtime – just ensure you set the logic in the SetConnectionInterface method in the code. In my example, I’m just getting an array of strings (from that Property) but this could really come from anywhere.
Enjoy!
Note:
If you intend to use the web part with PerformancePoint, do note that Filters will not work when you add the ‘Apply Filters’ button. I will cover this in a later post but basically you don’t build a standard web part if you want the full PerformancePoint experience.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Web.UI; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.WebPartPages; using System.Linq; namespace ConsumerWebPart.SampleWebPart { [ToolboxItemAttribute(false)] public class SampleWebPart : Microsoft.SharePoint.WebPartPages.WebPart { private List providers = new List(); [WebBrowsable(true), WebDisplayName("Filter EndPoints"), WebDescription(""), Personalizable(PersonalizationScope.Shared), Category("Custom Properties"), DefaultValue("")] public string FilterEndPoints { get; set; } protected override void Render(HtmlTextWriter writer) { string str = ""; foreach (IFilterValues provider in this.providers) { if (provider != null) { string prop = provider.ParameterName; ReadOnlyCollection values = provider.ParameterValues; if (prop != null && values != null) { str += SPEncode.HtmlEncode(prop) + "="; foreach (string v in values) { if (!string.IsNullOrEmpty(v)) str += v.Replace("&", "%26"); str += "&"; } } } } writer.Write(str); } [ConnectionConsumer("Filter Values", "IFilterValues", AllowsMultipleConnections = true)] public void SetConnectionInterface(IFilterValues provider) { this.providers.Add(provider); if (provider != null) { List l = new List(); foreach (string parm in this.GetParameters()) { l.Add(new ConsumerParameter(parm, ConsumerParameterCapabilities.SupportsMultipleValues | ConsumerParameterCapabilities.SupportsAllValue)); } provider.SetConsumerParameters(new ReadOnlyCollection(l)); } } private List GetParameters() { List parmList = new List(); string[] parms = this.FilterEndPoints.Split(';'); if (parms.Length > 0) parmList = parms.ToList(); return parmList; } } } |
Unable to load first class Dao provider type – PerformancePoint Services error code 10201 SPSite, FileNotFoundException and the Server Object Model in Console Applications and PowerShell Scripts