kamagra how much to take

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:

  1. if
  2. {
  3. myMenuItem.Enabled = true;
  4. }
  5. else
  6. {
  7. myMenuItem.Enabled = false;
  8. }

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:

  1. testText = testText + @"\ testing";
  2. test = new Microsoft.SharePoint.WebPartPages.MenuItem("testing", "javascript:alert('" + jessicaSimpsonText + @"'.replace('','\n'));");
  3. test.Visible = true;
  4. test.Enabled = true;
  5. this.WebPartMenu.MenuItems.Add(test);

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.

  1. public override WebPartVerbCollection Verbs
  2. {
  3. get
  4. {
  5. WebPartVerb verb = new WebPartVerb("testing", "javascript:alert('testing');");
  6. verb.Text = "testing";
  7. WebPartVerb[] newVerbs = new WebPartVerb[] { verb };
  8. WebPartVerbCollection verbs = new WebPartVerbCollection(base.Verbs, newVerbs);
  9. return verbs;
  10. }
  11. }

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.



  1. Ketan says:

    Thanks for the great article. Can you please let me know that in case if the additional webpart verbs are to be implemented for out of the box webparts (sharepoint) how can we do it, does it mean we will have to override the webparts.

  2. devshed says:

    good article, however, it is not complete, i.e. you say that there are two ways of creating custom menu for web part depending on the class the web part is inheriting from, but you fail to demonstrate complete examples.

  3. devshed says:

    sorry, I was quick to respond. My bad.


  1. SharePoint 2007 link love 4-25-2007 at Virtual Generations - [...] WebPart Verbs, MenuItem Class, and WebPart Menus [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>