次の方法で共有


方法: SharePoint プロジェクト項目の拡張機能を作成する

既に Visual Studio にインストールされている SharePoint プロジェクト項目に機能を追加する必要がある場合は、プロジェクト項目の拡張機能を作成します。 詳細については、「SharePoint プロジェクト項目の拡張」を参照してください。

プロジェクト項目の拡張機能を作成するには

  1. クラス ライブラリ プロジェクトを作成します。

  2. 次のアセンブリへの参照を追加します。

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

  3. ISharePointProjectItemTypeExtension インターフェイスを実装するクラスを作成します。

  4. クラスに次の属性を追加します。

  5. Initialize メソッドの実装では、projectItemType パラメーターのメンバーを使用して拡張機能の動作を定義します。 このパラメーターは、ISharePointProjectItemEvents インターフェイスおよび ISharePointProjectItemFileEvents インターフェイスに定義されているイベントにアクセスできる ISharePointProjectItemType オブジェクトです。 拡張しているプロジェクト項目の種類の特定インスタンスにアクセスするには、ProjectItemAddedProjectItemInitialized などの ISharePointProjectItemEvents イベントを処理します。

次のコード例に、イベント レシーバー プロジェクト項目の単純な拡張機能の作成方法を示します。 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 Extension (VSIX) パッケージを作成します。 詳細については、「Visual Studio での SharePoint ツールの拡張機能の配置」を参照してください。

参照

処理手順

チュートリアル: SharePoint プロジェクト項目の種類の拡張

その他の技術情報

SharePoint プロジェクト項目の拡張