Bagikan melalui


How to: Add a Shortcut Menu Item to a SharePoint Project Item Extension

You can add a shortcut menu item to an existing SharePoint project item by using a project item extension. The menu item appears when a user right-clicks the project item in Solution Explorer.

The following steps assume that you have already created a project item extension. For more information, see How to: Create a SharePoint Project Item Extension.

To add a shortcut menu item in a project item extension

  1. In the Initialize method of your ISharePointProjectItemTypeExtension implementation, handle the ProjectItemMenuItemsRequested event of the projectItemType parameter.

  2. In your event handler for the ProjectItemMenuItemsRequested event, add a new IMenuItem object to the ViewMenuItems or AddMenuItems collection of the event arguments parameter.

  3. In the Click event handler for the new IMenuItem object, perform the tasks you want to execute when a user clicks your shortcut menu item.

Example

The following code example demonstrates how to add a shortcut menu item to the Event Receiver project item. When the user right-clicks the project item in Solution Explorer and clicks the Write Message to Output Window menu item, Visual Studio displays a message in the Output window.

Imports System
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint

Namespace Contoso.Examples.ProjectItemExtensionWithMenu

    <Export(GetType(ISharePointProjectItemTypeExtension))> _
    <SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")> _
    Friend Class ExampleProjectItemExtensionWithMenu
        Implements ISharePointProjectItemTypeExtension

        Private Sub Initialize(ByVal projectItemType As ISharePointProjectItemType) _
            Implements ISharePointProjectItemTypeExtension.Initialize
            AddHandler projectItemType.ProjectItemMenuItemsRequested, AddressOf ProjectItemMenuItemsRequested
        End Sub

        Private Sub ProjectItemMenuItemsRequested(ByVal Sender As Object,
            ByVal e As SharePointProjectItemMenuItemsRequestedEventArgs)
            Dim menuItem As IMenuItem = e.ViewMenuItems.Add("Write Message to Output Window")
            AddHandler menuItem.Click, AddressOf MenuItem_Click
        End Sub

        Private Sub MenuItem_Click(ByVal Sender As Object, ByVal e As MenuItemEventArgs)
            Dim projectItem As ISharePointProjectItem = CType(e.Owner, ISharePointProjectItem)
            projectItem.Project.ProjectService.Logger.WriteLine(
                String.Format("This message was written from a shortcut menu for {0}.", projectItem.Name),
                LogCategory.Status)
        End Sub
    End Class
End Namespace
using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;

namespace Contoso.Examples.ProjectItemExtensionWithMenu
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")]
    internal class ExampleProjectItemExtensionWithMenu : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectItemMenuItemsRequested += projectItemType_ProjectItemMenuItemsRequested;
        }

        void projectItemType_ProjectItemMenuItemsRequested(object sender, 
            SharePointProjectItemMenuItemsRequestedEventArgs e)
        {
            IMenuItem menuItem = e.ViewMenuItems.Add("Write Message to Output Window");
            menuItem.Click += MenuItemExtension_Click;
        }

        void MenuItemExtension_Click(object sender, MenuItemEventArgs e)
        {
            ISharePointProjectItem projectItem = (ISharePointProjectItem)e.Owner;
            projectItem.Project.ProjectService.Logger.WriteLine(
                String.Format("This message was written from a shortcut menu for {0}.", projectItem.Name), 
                LogCategory.Status);
        }
    }
}

This example uses the SharePoint project service to write the message to the Output window. For more information, see Using the SharePoint Project Service.

Compiling the Code

This example requires a class library project with references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

Deploying the Extension

To deploy the extension, create a Visual Studio extension (VSIX) package for the assembly and any other files that you want to distribute with the extension. For more information, see Deploying Extensions for the SharePoint Tools in Visual Studio.

See Also

Tasks

Walkthrough: Extending a SharePoint Project Item Type

Concepts

How to: Create a SharePoint Project Item Extension

How to: Add a Property to a SharePoint Project Item Extension

Extending SharePoint Project Items