Web Part verbs in SharePoint 2010
Introduction
In this post we will see what are Web Part verbs in SharePoint 2010 and how we can use it.
Web Part Verb is user command that will be rendered in the Web Part menu that will be rendered on the right side of the title bar.
Default verbs
There are several default verbs like Minimize, Maximize, Close and Edit Web Part.
These default verbs can disabled by setting the properties either in Web Part file are from the code
To disable from xml
this.AllowEdit = false; this.AllowMinimize = false;
To disable from c#, set the available default propertied in Web Part file
this.AllowEdit = false; this.AllowMinimize = false;
If we set these properties, the menu in the Web Part as in above image will not be displayed
Note that these default Web Part verbs will only affect display mode but not edit mode. We can only disable the default verbs in the display mode. Once respective user with permissions, edit a page he will still see the default verbs.
Custom verbs
We can extend the Web Part menu with custom commands. The menu command behind a Web Part verb can be configured as either a client-side event handler or a server-side event handler. SharePoint 2010 also allows us to combine a client-side event handler and a server-side handler using a single Web Part verb.
To add one or more custom Web Part verbs to a Web Part class, we must override the read-only Verbs property.
We will take an example, to add simple custom verb name “Toggle Title”. When the verb is clicked, the title should be toggled i.e if visible it will be hidden, if hidden it will be visible.
Example
Create a simple Visual Web Part with boolean property showTitle.
In the Web Part file that is created, add the following override method
public override WebPartVerbCollection Verbs { get { //server-side verb WebPartVerb titleVerb = new WebPartVerb(this.ID + "_titleVerb", OnVerbExecuted); titleVerb.Text = "Toggle Title"; // return collection with new verbs WebPartVerb[] verbs = new WebPartVerb[] { titleVerb }; return new WebPartVerbCollection(verbs); } }
This will add “Toggle Title” in the menu
Now we have to add the event when the Toggle Title verb is clicked.
// event gets executed when the user invokes the server-side verb void OnVerbExecuted(object sender, WebPartEventArgs e) { SPWeb site = SPContext.Current.Web; SPFile page = site.GetFile(Context.Request.Url.AbsolutePath); SPLimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.User); VisualWebPart1 webpart = wpm.WebParts[this.ID] as VisualWebPart1; //Toggle the webpart title if (!webpart.showTitle) { webpart.showTitle = true; } else if (webpart.showTitle) { webpart.showTitle = false; } wpm.SaveChanges(webpart); }
This event will be executed when the verb “Toggle Title” is clicked. In the event handler we have to update the property of Web Part with SPLimitedWebPartManager. The above code will toggle the Title label
Conclusion
In this way we can set a custom verb to the webpart and thus it will be very useful to implement any property. Users can feel very comfortable rather than editing webpart and setting the property on the fly.
May 12, 2012
·
Adi ·
No Comments
Tags: Web Part verbs · Posted in: Sharepoint 2010
Leave a Reply