Web part verbs
The web part verbs are the menus items (like Minimize, Close etc) which appears when we click the Edit option in web parts as shown in the figure below. The SharePoint web part framework exposes one of the property called WebPartVerbCollection. This is very flexible which allows us to add our own items (custom web part verbs) to the web part collection.
Types of web part verbs
Verbs are objects of type WebPartVerb and can be of three different kinds:
1) Server-side Verbs that require a POST-back to carry out their job; they work on the server side.
2) Client-side Verbs that simply use JavaScript syntax to do their job; they work on the client side.
3) Client and server-side Verbs that first execute some client-side JavaScript, and then can execute some server-side code, unless the client-side code cancels request.
Following is the complete code for implementing verbs.
4) Deploy the web part and click on the webpart menu. You will notice that all three custom verbs are now displayed in the menu.
The web part verbs are the menus items (like Minimize, Close etc) which appears when we click the Edit option in web parts as shown in the figure below. The SharePoint web part framework exposes one of the property called WebPartVerbCollection. This is very flexible which allows us to add our own items (custom web part verbs) to the web part collection.
Types of web part verbs
Verbs are objects of type WebPartVerb and can be of three different kinds:
1) Server-side Verbs that require a POST-back to carry out their job; they work on the server side.
2) Client-side Verbs that simply use JavaScript syntax to do their job; they work on the client side.
3) Client and server-side Verbs that first execute some client-side JavaScript, and then can execute some server-side code, unless the client-side code cancels request.
Creating a custom web part verb
1) Create a new classic webpart and add the following code in the CreateChildControls() event. Deploy the webpart.
2) Following content is displayed in the webpart. Note that only default verbs are included as highlight in the following figure.
3) Now lets add the custom verb. We have to override the read-only property Verbs provided by the base WebPart class. Modified the code in webpart file as highlighted below. We have added three verbs:
i) 'Server-side verb' which will only raise a server side post-back.
ii) 'Client-side verb' which will pop up a JavaScript alert.
iii) 'Client and Server-side verb' which will raise a JavaScript alert and then a server side post-back.
Code Snippet
- protected override void CreateChildControls()
- {
- this.Controls.Add(new LiteralControl("<h1>Custom WebPart Verbs Demo!</h1>"));
- }
- public override WebPartVerbCollection Verbs
- {
- get
- {
- // Create a Server side verb
- WebPartVerb serverSideVerb = new WebPartVerb("serverSiteVerbId", handleServerSideVerb);
- serverSideVerb.Text = "Server-side verb";
- // Create a Client side verb
- WebPartVerb clientSideVerb = new WebPartVerb("clientSideVerbId", "javascript:alert('Client-side Verb selected');");
- clientSideVerb.Text = "Client-side verb";
- WebPartVerb clientAndServerSideVerb = new WebPartVerb("clientAndServerSideVerbId", handleServerSideVerb, "javascript:alert('Client-side Verb selected');");
- // Create a Client and Server side verb
- clientAndServerSideVerb.Text = "Client and Server-side verb";
- WebPartVerbCollection newVerbs = new WebPartVerbCollection(new WebPartVerb[] {serverSideVerb, clientSideVerb, clientAndServerSideVerb,});
- return (new WebPartVerbCollection(base.Verbs, newVerbs));
- }
- }
- protected void handleServerSideVerb(Object source, WebPartEventArgs args)
- {
- EnsureChildControls();
- }
4) Deploy the web part and click on the webpart menu. You will notice that all three custom verbs are now displayed in the menu.
5) Clicking on the 'Server-side verb' link will only raise a server side post-back.
6) Clicking on the 'Client-side verb' link will pop up a JavaScript alert.
7) Clicking on the 'Client and Server-side verb' link will pop up a JavaScript alert and then raise a server side post-back.
Note
1) Usually, custom Verbs are defined in Intranet/Extranet solutions, to provide support for custom functionalities, such as refreshing content, opening custom pop-up windows, and so forth.
2) In general, they are not used in CMS/Publishing solutions, because the context menu is usually disabled in these.
References
1) Microsoft SharePoint 2013 Developer Reference, by Paolo Pialorsi - Book
can we use this same in office 365
ReplyDelete