This is the first part of a multipart blog series.
Blog Series Summary:
As a part of a proof of concept site I was creating in SharePoint, I wanted to learn how to create a web part in SharePoint that can communicate to other web parts on the page. The web part that I wanted to create was a drop-down list box that would display a list of projects. Selecting a project on the drop-down list would then drive the contents of the various SharePoint project lists (issues, notes, risks, etc…) on the SharePoint page.
Part Three:
In part three of this blog, we’ll add the capabilities to use the selected value in the dropdown list box to filter various SharePoint lists on the page.
Step 1 – To make a filter provider, we are going to implement the interface ITransformableFilterValues. Update your class definition to include the ITransformableFilterValues interface.
public class DropDownFilterBlog : System.Web.UI.WebControls.WebParts.WebPart, ITransformableFilterValues
Step 2 – Now we’ll need to implement the various properties of the ITransformableFilterValues interface.
public bool AllowAllValue{get { return false; }}public bool AllowEmptyValue{get { return true; }}public bool AllowMultipleValues{get { return false; }}public string ParameterName{get{if ((this.ValueField != null) && (this.ValueField.Length > 0)){return this.ValueField;}else{return "Unknown";}}}public System.Collections.ObjectModel.ReadOnlyCollection<string> ParameterValues{get{string selectedValue = String.Empty;if ((_sourceDropDownList!=null) && (_sourceDropDownList.SelectedItem != null)){selectedValue = _sourceDropDownList.SelectedItem.Value;}string[] values = new string[] { selectedValue };return new ReadOnlyCollection<string>(values);}}
AllowAllValue, AllowEmptyValue and AllowMultipleValues are pretty straightforward. Since we are dealing with a dropdown list box we can only have one value selected. ParameterName is set to return a dynamic value based upon what you have set as the dropdown list box value property. The value of ParameterName is displayed in the Web Part Connection property editor. ParameterValues is the value sent to any consumer web parts (i.e. this is the value selected in the dropdown list box that we will pass to other web parts on the page).
using statements needs – The ReadOnlyCollection reference will require the following to be added to your list of using statements at the top of the class.
using System.Collections.ObjectModel;
Step 3 – And finally, we just need to a method that creates an instance of our filter provider.
[ConnectionProvider("DropDown Filter", "ITransformableFilterValues", AllowsMultipleConnections = true)]public ITransformableFilterValues SetConnectionInterface(){return this;}
The ConnectionProvider has multiple overloads. The first parameter shows up in the edit web part Connection menu option. The second parameter is the name that shows ups in the web part connections editor. The third parameters allows this web part to send it’s filter information to multiple consumers.
Step 4 – If you have not done so, set the URL to your SharePoint server: Right click project -> Properties -> Debug -> Start browser with URL.
Step 5 – Build and Deploy the web part to your SharePoint server. Right click project –> Deploy
Step 6 – If you have not down so, add the web part to a page and set the Dropdown filter properties to a SharePoint list.
Step 7 – Create a couple of SharePoint lists that have lookup values to the SharePoint lists you specified in Step 6.
Step 8 – Add some data to those lists.
Step 9 – Add those lists to your web page.
Step 10 – Set connections to your two lists from the DropDown Filter Web Part. From your Dropdown Filter Web Part edit menu: edit->Connections->Send DropDown Filter To –>YourList
And then Connection Editor appears. Match the field from your dropdown (in bold) to the appropriate field to filter on in the list. Note if then field you want to field on is not available, you might need to edit the view of your list to add the field. Special note, if you list is from a BDC Web Part list then you can filter any field from the BDC and not just those that are showing.
Do this for both lists on your SharePoint Web Part page.
Step 11 – Now as you change values in the dropdown list you should see the other lists filtering down to the selected item.
And that concludes part three. In part four, you can download the entire Visual Studio project.
» Similar Posts
- Dropdown Filter Web Part in SharePoint – Part Two
- Dropdown List Box Filter Web Part in SharePoint – Part One
- Creating a Lookup List for SharePoint with VSeWSS
» Trackbacks & Pingbacks
http://bilbroblog.com/trackback.ashx?id=9
» Comments
-
How would you know from what site the list will be taken from?
I've copied your code and can't seem to display the items in the dropdown list.
Thanks.
Jess — February 4, 2009 2:01 AM -
As the sample is currently written, it assumes the list is in the current site. However, it could be modified to access a list from a different site.
The key code to look at is the property SourceList.
There are you few different ways you could expand this sample. You could add another property called SiteTitle. If blank, then just assume the current site you are in. If not blank, just use the SharePoint API to access that site and from there access the list.
Here's a link for some different way of access SharePoint lists:
http://www.a2zdotnet.com/View.aspx?id=100
And here is an example of how to retrieve the list data from a different site:
SPSite spSite = new SPSite("http://YourServer/YourSite/");
SPWeb spWeb = spSite.OpenWeb();
SPList spList = spWeb.Lists["YourListTitle"];
That would be just one way to do it.
Cheers,
Brian
Brian Bilbro — February 4, 2009 8:46 AM -
Mate, that was a beautifully written tutorial!
It's amazing how often crucial parts are left out or how difficult something simple is made to appear, particularly with MSDN articles!
Cheers for that and keep up the good work!
Have you thought about doing a 'how to' on how to integrate the above with a Content Query Web Part? I only say that because I'm about to attempt it myself!
Brian — April 13, 2009 10:18 PM -
I used your example (steps 1 - 3) to create a custom filter web part. The custom web part has selection buttons. Each button has multiple values attached to it. So, AllowMultipleValues is true. When clicking the button, the property ReadOnlyCollection<string> ParameterValues will collect multiple values, which are then broadcasted to different data view web parts on the same page. The custom filter web part is deployed to SharePoint server without problem and it appears on the page. Now, when I connect this filter web part to other data view web parts (i.e., the step 10), there is runtime error: Access is denied. When using “Visual Studio Just-In-Time Debugger” to debug, the code stops at this statement: var returnInfo=window.showModalDialog(xFormUrl, null, sFeatures) in the file ie55up.js. Just wonder whether you met the problem before. Thanks.
PC
PC — November 5, 2009 12:40 PM -
perfect article. Thank you.
georgi danchev — February 5, 2010 2:53 AM -
Hi,
I've a question about getting the Lists in a dropdown menu. E.g under the Header Lists I've these items. The structure is this:
Lists
- Calendar
- Projects
- TimeSheets
- Competitors
- Sales Leads
- Testlist
How do I get Calendar, Projects, Timesheets.. etc.. in a dropdown menu?
I really appriciate your help.
Thanks in advance.
ux — March 31, 2010 8:31 AM -
Please could you tell us which files each of these pieces of code go into, .cs or ascx.cs
Joe — July 27, 2010 5:48 AM -
@Joe
If you have trouble following where to add the code, you can download the complete source code in part four.
Brian Bilbro — July 27, 2010 8:01 AM