Monday, 25 May 2015

Create Custom Webpart Menu item ( Verbs ) like Edit web part in SharePoint 2013 using C#

Problem Description:

We have the requirement to add new webpart menu like below pic. On click of the Custom menu we have to associate to a Javascript which will open SharePoint popup page.

Solution : 

I searched a lot on google try to figure out how we can add custom menu to the webpart with the Keyword "Create Custom Webpart Menu in SharePoint 2010" . but i could not find any result on first two three pages of google Result. finally i found in some page this webpart menu items are called as Webpart Verbs. Once i get this (Verbs)  Keyword it was easy for me to get the piece of code from google. I am just writing this if someone like me doesn't know about the Webpart Verbs , for them it might be useful.

Code :

Add the below code in your Webpart.cs file, it should create a Custom menu as shown in above figure.

   public override WebPartVerbCollection Verbs
                var yourVerb = new WebPartVerb(ID + "yourVerb ", "showModel('"+ID+"')") { Text = "Edit List View webpart" };
                var verbs = new[] { yourVerb };
                return new WebPartVerbCollection(base.Verbs, verbs);


We have to Override the WebpartCollection Verbs property and add new custom verb to the collection. In the Above method, i have "showModel" Javascript method which will be called on click of the Menu "Edit List View webpart". You can write either server side code or Java script function against the custom menu.

Hope this article will help to create a custom menu in the webpart.

Ask me if you have any doubt.

No comments:

Post a Comment