Bagikan melalui


How to: Add a Shortcut Menu Item to a Custom SharePoint Project Item Type

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 clicks 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 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.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
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);
        }
    }
}

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 Project Item

To enable other developers to use your project item, create a project template or a project item template. For more information, see Creating 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 Deploying Extensions for the SharePoint Tools in Visual Studio.

See Also

Concepts

How to: Define a SharePoint Project Item Type

How to: Add a Property to a Custom SharePoint Project Item Type

Defining Custom SharePoint Project Item Types