共用方式為


HOW TO:將捷徑功能表項目加入至自訂 SharePoint 專案項目類型

當您定義自訂 SharePoint 專案項目類型時,您可以將捷徑功能表項目加入至專案項目。 使用者在 [方案總管] 中以滑鼠右鍵按一下專案項目時,功能表項目就會出現。

下列步驟假設您已經定義自己的 SharePoint 專案項目型別。 如需詳細資訊,請參閱 HOW TO:定義 SharePoint 專案項目類型

若要將捷徑功能表項目加入至自訂專案項目類型

  1. 在您的 ISharePointProjectItemTypeProvider 實作之 InitializeType 方法中,請處理 projectItemTypeDefinition 參數的 ProjectItemMenuItemsRequested 事件。

  2. ProjectItemMenuItemsRequested 事件的事件處理常式中,將新的 IMenuItem 物件加入至事件引數參數的 ViewMenuItemsAddMenuItems 集合。

  3. 在新 IMenuItem 物件的 Click 事件處理常式中,執行在使用者按一下捷徑功能表項目時要執行的工作。

範例

下列程式碼範例示範如何將內容功能表項目加入至自訂專案項目型別。 當使用者以滑鼠右鍵按一下 [方案總管] 中的專案項目,並且按一下 [將訊息寫入輸出視窗] 功能表項目時,Visual Studio 會在 [輸出] 視窗中顯示訊息。

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

此範例使用 SharePoint 專案服務來將訊息寫入 [輸出] 視窗。 如需詳細資訊,請參閱 使用 SharePoint 專案服務

編譯程式碼

這個範例需要類別庫專案並參考下列組件:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

部署專案項目

若要讓其他開發人員使用您的專案項目,請建立專案範本或專案項目範本。 如需詳細資訊,請參閱 為 SharePoint 專案項目建立項目範本和專案範本

若要部署專案項目,請針對組件、範本以及要與專案項目一起散發的任何其他檔案建立 Visual Studio Extension (VSIX) 套件。 如需詳細資訊,請參閱 部署 Visual Studio 中 SharePoint 工具的擴充功能

請參閱

工作

HOW TO:定義 SharePoint 專案項目類型

其他資源

HOW TO:將屬性加入至自訂 SharePoint 專案項目類型

定義自訂 SharePoint 專案項目類型