Creating WebPart Verbs

A verb, when talking about webparts, is a menu item that appears when you click the down arrow in the webpart's title bar. Typically, you'll see Minimize, Close, and Modify Shared Web Part. To add your own verbs is pretty simple.

The first thing you'll want to do is override the Verbs property. When you do this, you get a public Verb property that contains only a get and returns a WebPartVerbCollection. You have the option of creating a client-side and/or server-side verb. In the following example, I create one of each.

First, I created a WebPartVerb object called clientVerb. When instantiating it, I gave it an ID and the client click handler which is just a javascript alert method that gives you some information about the webpart. You might want to give ID a better name than the one I gave mine. You'll want to give clientVerb values for Text and Description. Text is displayed in the menu and description will display as a tool tip when you hover over the item.

Then, I created another WebPartVerb called serverVerb. I gave this one an ID and a WebPartEventHandler when instantiating it. Then I set the Text that will appear in the menu. In this case "SharePoint Lessons (server)".

Finally, I created an array of WebPartVerb to store the two verbs created and instantiated a WebPartVerbCollection with base.Verbs and my array of verbs. The following image is the final result of the Verbs Property.

Here is my server side event handler. As you can see, all it does is redirects the user to this blog.
When I deploy my solution, you'll see my two menu items appear at the end of my webpart's menu.


Here is the result when I click on "About WebPart (client)".


You can use this information to add information about your webpart or company. Another thing you might want to do is remove the default verbs. If you have some webparts that do heavy processing, you may not want the user to be able to just close the webpart since close is really just a hide and the webpart is still running in the background. If you don't want the default verbs to show, just remove the base.Verbs parameter from the WebPartVerbCollection.

Labels: