WebPart Verbs, MenuItem Class, and WebPart Menus

Some people testing some of my free WebParts were wondering how the WebPart debug information was added to a WebPart menu as opposed to creating a display property (as what’s done in the Redirector WebPart) that was controlled through the WebPart tool pane.

I guess that there are two ways you could do it, either through WebPart verbs (which is probably the best way because its native ASP.NET 2.0) or through the use of the Microsoft.SharePoint.WebPartPages namespace reference which sponsors the MenuItem class. If you are using the best practice of using the ASP.NET WebPart base class for SharePoint development (which is probably your best plan per popular vote, I could honestly care less) you can use the WebPartVerb class and the WebPartEventHandler to call some sort of server-side functionality when the item is selected from the WebPart menu to pop out the information via a JavaScript call with some server side rendering code. Since the verbs that a WebPart can use are sponsored by the WebPartVerbCollection, a property of the WebPart, all you need to do with this approach is add an WebPart verb into the WebPart verb array, add the array to that collection, and you are ready to go.

There are a few classes for this simple example that I am about to expose you to that if you have be doing past SharePoint development for a while you might not be so hot on. Ill breeze through them so that they make sense when you see them in a bit.

WebPartVerb – The WebPart Verb class inherits from the IStateManager interface which is responsible for view state management of WebParts (server controls). Each WebPart Verb object can be added to the WebPartVerbCollection.

WebPartVerbCollection – This class inherits from the ReadOnlyCollectionBase abstract base class which allows a collection of strongly typed objects. The WebPartVerbCollection is responsible for the collection of WebPart verbs.

The cool thing about using WebPart verbs is they have some very, very enhanced functionality over the SharePoint MenuItem class. Implementing the IWebActionable interface (since it allows a WebPart to contain a collection of verbs), ASP.NET WebPart verbs can even be extended to add custom verbs even to controls, which I guess is neat. There are some pretty important WebPart verb properties that should be taken into consideration when working with ASP.NET 2.0 WebPart verbs:

Checked – Tells whether the state of the verb is active (if the little check box appears after it has been clicked).
Description – Tells a little something about the WebPart that is displayed in the ToolTip

EnabledWhether the WebPart is selectable (grayed out or not). You can set conditions on this as well with simple if statements if you want WebPart verbs conditionally selectable, such as submitted a post back or something for data retrieval. It is just something like:

myMenuItem.Enabled = true;
myMenuItem.Enabled = false;


ImageUrl – The image to display with the WebPart verb. Store them with the base.ClassResourcePath for Christ sake. I hate people that hard code image paths and I have to deal with permissions issue on the file store. It drives me insane.

Text – The WebPart verb text, you know, how it displays on the menu or whatever
Visible – Indicates whether the WebPart verb is visible. Its really not that fancy, just declare your verb as myMenuItem.Visible = true;

Either method will work, either MenuItem or WebPart verbs, obviously assuming that you have the appropriate references established, and what base class you are inheriting out of. Both of these simple examples use JavaScript just to parse out an alert menu with the text “testing”.

MenuItem Class Example
So you can normally just use this class and it works kind of similar to the concept of overriding / adding your own child controls:


testText = testText + @”\ testing”;
test = new Microsoft.SharePoint.WebPartPages.MenuItem(“testing”, “javascript:alert(‘” + jessicaSimpsonText + @”‘.replace(”,’\n’));”);
test.Visible = true;
test.Enabled = true;


ASP.NET 2.0 WebPart Verb Example
Now that we have gone old-school with the menu creation, something all us SharePoint developers are familiar with, you can also create the same type of functionality using ASP.NET 2.0 WebPart verbs.


public override WebPartVerbCollection Verbs
WebPartVerb verb = new WebPartVerb(“testing”, “javascript:alert(‘testing’);”);
verb.Text = “testing”;
WebPartVerb[] newVerbs = new WebPartVerb[] { verb };
WebPartVerbCollection verbs = new WebPartVerbCollection(base.Verbs, newVerbs);
return verbs;

It is really up to you whichever you want to use, and how your WebPart is coded. I might do a continuation of advanced server side coding with WebPart verbs at a later date.