HOW TO:建立 SharePoint 專案項目擴充功能
如果您要將功能加入至 Visual Studio 中已安裝的 SharePoint 專案項目時,請建立專案項目擴充功能。如需詳細資訊,請參閱擴充 SharePoint 專案項目。
若要建立專案項目擴充功能
建立類別庫專案。
加入下列組件的參考:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
建立實作 ISharePointProjectItemTypeExtension 介面的類別。
將下列屬性加入到類別:
System.ComponentModel.Composition.ExportAttribute.此屬性可讓 Visual Studio 探索並載入 ISharePointProjectItemTypeExtension 實作。將 ISharePointProjectItemTypeExtension 型別傳遞至屬性建構函式。
SharePointProjectItemTypeAttribute.在專案項目擴充功能中,此屬性會識別您要擴充的專案項目。請將專案項目的 ID 傳遞至屬性建構函式。專案項目所包含的Visual StudioId 的清單,請參閱擴充 SharePoint 專案項目。
在 Initialize 方法的實作中,使用 projectItemType 參數的成員來定義擴充功能的行為。這個參數是 ISharePointProjectItemType 物件,可用來存取 ISharePointProjectItemEvents 和 ISharePointProjectItemFileEvents 介面中定義的事件。若要存取您要擴充的特定專案項目類型執行個體,請處理 ISharePointProjectItemEvents 事件,例如 ProjectItemAdded 和 ProjectItemInitialized。
範例
下列程式碼範例示範如何為 [事件接收器] 專案項目建立簡單的擴充功能。每當使用者將 [事件接收器] 專案項目加入至 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 工具的擴充功能。