How to: Add a shortcut menu item to a custom SharePoint project item type

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

When you define a custom SharePoint project item type, you can add a shortcut menu item to the project item. The menu item appears when a user right-clicks the project item in Solution Explorer.

The following steps assume that you have already defined your own SharePoint project item type. For more information, see How to: Define a SharePoint project item type.

To add a shortcut menu item to a custom project item type

  1. In the InitializeType method of your ISharePointProjectItemTypeProvider implementation, handle the ProjectItemMenuItemsRequested event of the projectItemTypeDefinition 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 chooses your shortcut menu item.

Example

The following code example demonstrates how to add a context menu item to a custom project item type. When the user opens the shortcut menu from the project item in Solution Explorer and chooses the Write Message to Output Window menu item, Visual Studio displays a message in the Output window.

using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;

namespace Contoso.Examples.ProjectItemTypeWithMenu
{
    [Export(typeof(ISharePointProjectItemTypeProvider))]
    [SharePointProjectItemType("Contoso.ExampleProjectItemType")]
    [SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")]
    internal class ExampleProjectItemTypeWithMenu : ISharePointProjectItemTypeProvider
    {
        public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition)
        {
            projectItemTypeDefinition.Name = "ExampleProjectItemType";
            projectItemTypeDefinition.SupportedDeploymentScopes =
                SupportedDeploymentScopes.Site | SupportedDeploymentScopes.Web;
            projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All;

            projectItemTypeDefinition.ProjectItemMenuItemsRequested += 
                projectItemTypeDefinition_ProjectItemMenuItemsRequested;
        }

        void projectItemTypeDefinition_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);
        }
    }
}
Imports System
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint

Namespace Contoso.Examples.ProjectItemTypeWithMenu

    <Export(GetType(ISharePointProjectItemTypeProvider))> _
    <SharePointProjectItemType("Contoso.ExampleProjectItemType")> _
    <SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")> _
    Friend Class ExampleProjectItemTypeWithMenu
        Implements ISharePointProjectItemTypeProvider

        Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _
            Implements ISharePointProjectItemTypeProvider.InitializeType
            projectItemTypeDefinition.Name = "ExampleProjectItemType"
            projectItemTypeDefinition.SupportedDeploymentScopes = _
                SupportedDeploymentScopes.Site Or SupportedDeploymentScopes.Web
            projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All

            AddHandler projectItemTypeDefinition.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

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

Compile the code

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

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

Deploy the project item

To enable other developers to use your project item, create a project template or a project item template. For more information, see Create item templates and project templates for SharePoint project items.

To deploy the project item, create a Visual Studio extension (VSIX) package for the assembly, the template, and any other files that you want to distribute with the project item. For more information, see Deploy extensions for the SharePoint tools in Visual Studio.

See also