SharePoint プロジェクトの配置時または取り消し時に別途タスクを実行する必要がある場合は、Visual Studio によって生成されるイベントを処理する方法があります。 詳細については、「SharePoint のパッケージ化と配置の拡張」を参照してください。
SharePoint プロジェクトの配置時または取り消し時にコードを実行するには
プロジェクト項目の拡張機能、プロジェクトの拡張機能、または新しいプロジェクト項目の種類の定義を作成します。 詳細については、次のトピックを参照してください。
拡張機能で、ISharePointProjectService オブジェクトにアクセスします。 詳細については、「方法: SharePoint プロジェクト サービスを取得する」を参照してください。
プロジェクト サービスの DeploymentStarted イベントと DeploymentCompleted イベントを処理します。
イベント ハンドラーで、DeploymentEventArgs パラメーターを使用して、現在の配置セッションに関する情報を取得します。 たとえば、現在の配置セッションにあるプロジェクトや、そのプロジェクトが配置中なのか取り消し中なのかを判別することができます。
プロジェクトの拡張機能で DeploymentStarted イベントと DeploymentCompleted イベントを処理する方法を示すコード例を次に示します。 SharePoint プロジェクトの配置が開始され、完了すると、この拡張機能によって [出力] ウィンドウに追加のメッセージが書き込まれます。
Imports System
Imports Microsoft.VisualStudio.SharePoint
Imports Microsoft.VisualStudio.SharePoint.Deployment
Imports System.ComponentModel.Composition
Namespace Contoso.ProjectDeploymentExtension
<Export(GetType(ISharePointProjectExtension))> _
Friend Class ExampleProjectDeploymentExtension
Implements ISharePointProjectExtension
Private Sub Initialize(ByVal projectService As ISharePointProjectService) _
Implements ISharePointProjectExtension.Initialize
AddHandler projectService.DeploymentStarted, AddressOf DeploymentStarted
AddHandler projectService.DeploymentCompleted, AddressOf DeploymentCompleted
End Sub
Private Sub DeploymentStarted(ByVal Sender As Object, ByVal e As DeploymentEventArgs)
If e.DeploymentContext.IsDeploying Then
Dim message As String = String.Format("Deployment started for the {0} project.",
e.Project.Name)
e.DeploymentContext.Logger.WriteLine(message, LogCategory.Status)
End If
End Sub
Private Sub DeploymentCompleted(ByVal Sender As Object, ByVal e As DeploymentEventArgs)
If e.DeploymentContext.IsDeploying Then
Dim message As String = String.Format("Deployment completed for the {0} project.",
e.Project.Name)
e.DeploymentContext.Logger.WriteLine(message, LogCategory.Status)
End If
End Sub
End Class
End Namespace
using System;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Deployment;
using System.ComponentModel.Composition;
namespace Contoso.ProjectDeploymentExtension
{
[Export(typeof(ISharePointProjectExtension))]
internal class ExampleProjectDeploymentExtension : ISharePointProjectExtension
{
public void Initialize(ISharePointProjectService projectService)
{
projectService.DeploymentStarted += ProjectService_DeploymentStarted;
projectService.DeploymentCompleted += ProjectService_DeploymentCompleted;
}
void ProjectService_DeploymentStarted(object sender, DeploymentEventArgs e)
{
if (e.DeploymentContext.IsDeploying)
{
string message = String.Format("Deployment started for the {0} project.",
e.Project.Name);
e.DeploymentContext.Logger.WriteLine(message, LogCategory.Status);
}
}
void ProjectService_DeploymentCompleted(object sender, DeploymentEventArgs e)
{
if (e.DeploymentContext.IsDeploying)
{
string message = String.Format("Deployment completed for the {0} project.",
e.Project.Name);
e.DeploymentContext.Logger.WriteLine(message, LogCategory.Status);
}
}
}
}
コードのコンパイル
この例は、次のアセンブリへの参照を必要とします。
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
拡張機能の配置
拡張機能を配置するには、アセンブリと、拡張機能に同梱する必要のあるその他のファイルを提供するための Visual Studio Extension (VSIX) パッケージを作成します。 詳細については、「Visual Studio での SharePoint ツールの拡張機能の配置」を参照してください。