Customizing Custom Web Part Menu in SharePoint

Any standard web part within SharePoint would have menu options like Minimize, Close, and Modify Shared Web Part. We can even add our own custom menu options over there. They are referred to as verbs in web part’s context. We could add three types of web part menu verbs.

Client Side –We could specify JavaScript over here.

Server Side – We can attach event handler over here.

Both – We could have both JavaScript and event handler specified.

Let’s take a simple example where we would be adding all the above three kind of verbs.

Create a web part project within Visual Studio.

Put the following code for web part,

It would have a text box that would show the text set using the event handler specified in the server side verb.

[Guid(“172bc4c1-1c5d-49cd-93bc-1874acbbb9c8”)]

    public class WebPartMenuVerbWebPart : System.Web.UI.WebControls.WebParts.WebPart

    {

        public WebPartMenuVerbWebPart()

        {

        }

 

        protected  TextBox txtInfo;

        protected override void CreateChildControls()

        {           

            txtInfo = new TextBox();

            this.Controls.Add(txtInfo);           

        }

        public override WebPartVerbCollection Verbs

        {

            get

            {

                // Client side verb

                WebPartVerb clientSideVerb = new WebPartVerb(“clientID”, “javascript:alert(‘Hello World from Java Script Verb!’);”);

                clientSideVerb.Text = “Client Side Verb”;

                // Server side verb

                WebPartVerb serverSideVerb = new WebPartVerb(“serverID”, new WebPartEventHandler(ServerVerbEventHandler));

                serverSideVerb.Text = “Server Side Verb”;

                // Verb for both client side and server side

                WebPartVerb bothSideVerb = new WebPartVerb(“bothID”, new WebPartEventHandler(ServerVerbEventHandler), “javascript:alert(‘Hello World from Java Script Verb!’);”);

                bothSideVerb.Text = “Both Side Verb”;          

                WebPartVerbCollection wbVerbCollection = new WebPartVerbCollection(base.Verbs, new WebPartVerb[] { clientSideVerb, serverSideVerb, bothSideVerb  });

                return wbVerbCollection;

            }

        }

        protected void ServerVerbEventHandler(object sender, WebPartEventArgs args)

        {

            txtInfo.Text=“Hello world from Server Side Verb”;                           

        }       

    }

 

That’s it …


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

4 thoughts on “Customizing Custom Web Part Menu in SharePoint”

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s