Customizing Web part shortcut menu (chrome)

Web part shortcut menu allows you to minimize, Close, Modify web part and export. To add new item, edit or modify existing item in menu you need to override CreateWebpartmenu method of WebPart base class and access the MenuItems collection of the Web Part’s WebPartMenu property.
List of Item IDs
• MSOMenu_Minimize
• MSOMenu_Restore
• MSOMenu_Close
• MSOMenu_Delete
• MSOMenu_Edit
• MSOMenu_Connections
• MSOMenu_Export
• MSOMenu_Help

Sample example

public override void CreateWebPartMenu()
{
MenuItem ParentItem, Item1, Item2;

//first we create three menu items 
//one as a parent item, and two submenu items 
//create the parent item 

ParentItem = new MenuItem("ParentItem", "", "ParentItemID");

//create a submenu item with a server event on click 
Item1 = new MenuItem("Item1", "Item1ID", new EventHandler(EventHandlerForItem1));

//create a submenu item with a client event on click 
Item2 = new MenuItem("Item2", "javascript:alert('Item2 was clicked');", "Item2ID");
// We then add the submenu items to the parent item. 


ParentItem.MenuItems.Add(Item1);
ParentItem.MenuItems.Add(Item2);

// We then decide that we'd like to add the parent item after the 
// "Edit Web Part Properties" item in the menu. 
// First we find the index of the "Edit" item 
int EditIndex =
this.WebPartMenu.MenuItems.IndexOf(this.WebPartMenu.MenuItems.ItemFromID("MSOMenu_Edit"));

// Then we insert the parent item after the "Edit" item 
this.WebPartMenu.MenuItems.Insert(EditIndex + 1, ParentItem);


// Make sure the parent item has a separator above it 
ParentItem.BeginSection = true;


// Check a custom property to decide whether to display the parent menu 
if (this.CustomProperty1 == true)
{
ParentItem.Visible = true;
}
else
{
ParentItem.Visible = false;
}
// Check another custom property to decide which menu item to enable 
// within the parent item 
if (this.CustomProperty2 == true)
{
Item1.Enabled = true;
Item2.Enabled = false;
}
else
{
Item1.Enabled = false;
Item2.Enabled = true;
}
}

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *