共用方式為


HOW TO:建立 SharePoint 專案項目擴充功能

如果您要將功能加入至 Visual Studio 中已安裝的 SharePoint 專案項目時,請建立專案項目擴充功能。如需詳細資訊,請參閱擴充 SharePoint 專案項目

若要建立專案項目擴充功能

  1. 建立類別庫專案。

  2. 加入下列組件的參考:

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

  3. 建立實作 ISharePointProjectItemTypeExtension 介面的類別。

  4. 將下列屬性加入到類別:

  5. Initialize 方法的實作中,使用 projectItemType 參數的成員來定義擴充功能的行為。這個參數是 ISharePointProjectItemType 物件,可用來存取 ISharePointProjectItemEventsISharePointProjectItemFileEvents 介面中定義的事件。若要存取您要擴充的特定專案項目類型執行個體,請處理 ISharePointProjectItemEvents 事件,例如 ProjectItemAddedProjectItemInitialized

範例

下列程式碼範例示範如何為 [事件接收器] 專案項目建立簡單的擴充功能。每當使用者將 [事件接收器] 專案項目加入至 SharePoint 專案時,此擴充功能都會將訊息寫入 [輸出] 視窗和 [錯誤清單] 視窗。

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

Namespace Contoso.ExampleProjectItemExtension

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

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

        Private Sub ProjectItemAdded(ByVal Sender As Object, ByVal e As SharePointProjectItemEventArgs)
            Dim projectItem As ISharePointProjectItem = CType(Sender, ISharePointProjectItem)
            Dim Message As String = String.Format("An Event Handler project item named {0} was added to the {1} project.", _
                projectItem.Name, projectItem.Project.Name)
            projectItem.Project.ProjectService.Logger.WriteLine(Message, LogCategory.Message)
        End Sub
    End Class
End Namespace
using Microsoft.VisualStudio.SharePoint;
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace Contoso.ExampleProjectItemExtension
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")]
    internal class ExampleProjectItemExtension : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectItemAdded += projectItemType_ProjectItemAdded;
        }

        void projectItemType_ProjectItemAdded(object sender, SharePointProjectItemEventArgs e)
        {
            ISharePointProjectItem projectItem = (ISharePointProjectItem)sender;
            string message = String.Format("An Event Handler project item named {0} was added to the {1} project.",
                projectItem.Name, projectItem.Project.Name);
            projectItem.Project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
        }
    }
}

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

編譯程式碼

這個範例需要參考下列組件:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

部署擴充功能

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

請參閱

工作

逐步解說:擴充 SharePoint 專案項目類型

概念

擴充 SharePoint 專案項目