MenuItemCollection class
TOC
Collapse the table of content
Expand the table of content

MenuItemCollection class

Used to add, delete, and arrange the menu items that are displayed in a Web Part menu.

System.Object
  System.Collections.CollectionBase
    Microsoft.SharePoint.WebPartPages.MenuItemCollection

Namespace:  Microsoft.SharePoint.WebPartPages
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

'Declaration
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class MenuItemCollection _
	Inherits CollectionBase
'Usage
Dim instance As MenuItemCollection

The following code example creates a custom menu that has one parent menu item and two submenu item. It assumes that this method is part of a Web Part class that includes an event handler named EventHandlerForItem1.

Public Overrides Sub CreateWebPartMenu()
    ' Declare variables for menu items.
    Dim ParentItem As MenuItem
    Dim Item1 As MenuItem
    Dim Item2 As MenuItem

    ' Create three menu items:
    ' One 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(AddressOf EventHandlerForItem1))

    ' Create a submenu item with a client event on click. 
    Item2 = New MenuItem("Item2", "javascript:alert('Item2 was clicked');", "Item2ID")

    ' Add the submenu items to the parent item.
    ParentItem.MenuItems.Add(Item1)
    ParentItem.MenuItems.Add(Item2)

    ' Add the parent item after the "Modify Shared/Personal Web Part"
    ' command in the default menu. 

    ' Retrieve the index of the "Modify Shared/Personal Web Part" 
    ' command.
    Dim EditIndex As Integer = Me.WebPartMenu.MenuItems.IndexOf(Me.WebPartMenu.MenuItems.ItemFromID("MSOMenu_Edit"))

    ' Insert the parent item after the "Modify Shared/Personal Web 
    ' Part" command.
    Me.WebPartMenu.MenuItems.Insert(EditIndex + 1, ParentItem)

    ' Add a separator above the parent item.
    ParentItem.BeginSection = True

    ' Check the "ParentItemIsVisible" custom boolean property to 
    ' decide whether to display the parent menu.
    If Me.ParentItemIsVisible = True Then
        ParentItem.Visible = True

    Else
        ParentItem.Visible = False
    End If

    ' Check the "EnableItem1" custom boolean property to decide which 
    ' menu item to enable. 
    ' within the parent item.
  If Me.EnableItem1 = True Then
        Item1.Enabled=True
        Item2.Enabled=False

    Else
        Item1.Enabled=False
        Item2.Enabled=True
    End If
End Sub

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2016 Microsoft