Dropdown option lists in webpart personalisation menus

I’ve been writing my own SharePoint webparts for a few months now and have been quite happy using personalisation options to provide a certain degree of control during runtime from the Edit Shared Webpart properties menu.

All of these controls have either been using a text box entry or a tick box boolean, Until now that is. I realised that I needed to have some form of control over what was being entered by the admin. After all, whilst it’s always good to make sure that you validate any user input, it makes sense to restrict that input to selected options when possible.

In my case, I want the admin to be able to choose between which teams to filter the inbound data on, this in turn was then fed into a CAML query to return the correct data for the webpart.

After much reading of logs and blogs, I posted a quick question on the SUGUK forum and one of the readers, Paul Leigh suggested I look at using an Enum to provide the drop down.

Quite intrigued, I brushed off my c# text book to remind me how to define an enum, and set off on the following code.

First we define the enum within the webpart class boundary.

Public enum resourceTypes : int
{
NR,
Projects,
Windows,
Networks,
Unix,

snipped......

FirstAssist
};

Now we need to define the public and private members of the class, You don’t have to do this, but it’s best practice!


private resourceTypes m_resourceFilter;

[Personalizable(PersonalizationScope.Shared), Category("ESSOP"), WebBrowsable(true), WebDisplayName("Select team to filter results by")
, WebDescription("Use NR value when not required.")]
public resourceTypes resourceFilter
{
get {return m_resourceFilter;}
set {m_resourceFilter = value;}
}

Thats really all there is to it, in my case, i evaluate the enumeration using the ToString method and push that into my query string.

NB: You can only use a single word in the enum, spaces aren’t allowed, so in order to get around some of my teams that have double barrelled names, I put a Select statement further down the code at the injection point into the CAML query….


switch (m_resourceFilter.ToString())
{
case "StorageBackup":
resourceSelection = "Storage & Backup";
break;

case "TechOps":
resourceSelection = "Tech Ops";
break;

case "FirstAssist":
resourceSelection = "First Assist";
break;

case "BusinessAssurance":
resourceSelection = "Business Assurance";
break;

default:
resourceSelection = m_resourceFilter.ToString();
break;
}

Hopefully this might give you some pointers to solve a similar issue in your projects.

Regards

Reg.

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.