This topic has not yet been rated - Rate this topic

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.

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.

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.

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

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.