次の方法で共有


方法: 配置手順の実行時にコードを実行する

SharePoint プロジェクトの配置手順に関して別途タスクを実行する必要がある場合は、Visual Studio が個々の配置手順を実行する前と後に SharePoint プロジェクト項目によって生成されるイベントを処理する方法があります。 詳細については、「SharePoint のパッケージ化と配置の拡張」を参照してください。

配置手順を実行するときにコードを実行するには

  1. プロジェクト項目の拡張機能、プロジェクトの拡張機能、または新しいプロジェクト項目の種類の定義を作成します。 詳細については、次のトピックを参照してください。

  2. 拡張機能で、ISharePointProjectItemType オブジェクト (プロジェクト項目の拡張機能またはプロジェクトの拡張機能) または ISharePointProjectItemTypeDefinition オブジェクト (新しいプロジェクト項目の種類の定義) の DeploymentStepStarted イベントおよび DeploymentStepCompleted を処理します。

  3. イベント ハンドラーで、DeploymentStepStartedEventArgs パラメーターおよび DeploymentStepCompletedEventArgs パラメーターを使用して、配置手順に関する情報を取得します。 たとえば、実行されている配置手順や、ソリューションが配置中なのか取り消し中なのかを判別することができます。

リスト インスタンス プロジェクト項目の拡張機能で、DeploymentStepStarted イベントおよび DeploymentStepCompleted イベントを処理する方法を次のコード例に示します。 この拡張機能は、ソリューションの配置中および取り消し中、Visual Studio によってアプリケーション プールがリサイクルされると、[出力] ウィンドウに追加のメッセージを出力します。

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

Namespace Contoso.ListInstanceDeploymentExtension

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

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

        Private Sub DeploymentStepStarted(ByVal Sender As Object, ByVal e As DeploymentStepStartedEventArgs)
            If e.DeploymentStepInfo.Id = DeploymentStepIds.RecycleApplicationPool AndAlso
                e.DeploymentContext.IsDeploying Then
                e.DeploymentContext.Logger.WriteLine("The application pool is about to be " &
                    "recycled while the solution is being deployed.", LogCategory.Status)
            End If
        End Sub

        Private Sub DeploymentStepCompleted(ByVal Sender As Object, ByVal e As DeploymentStepCompletedEventArgs)
            If e.DeploymentStepInfo.Id = DeploymentStepIds.RecycleApplicationPool AndAlso
                e.DeploymentContext.IsRetracting Then
                e.DeploymentContext.Logger.WriteLine("The application pool was " &
                    "recycled while the solution is being retracted.", 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.ListInstanceDeploymentExtension
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListInstance")]
    internal class ExampleDeploymentStepExtension : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.DeploymentStepStarted += DeploymentStepStarted;
            projectItemType.DeploymentStepCompleted += DeploymentStepCompleted;
        }

        private void DeploymentStepStarted(object sender, DeploymentStepStartedEventArgs e)
        {
            if (e.DeploymentStepInfo.Id == DeploymentStepIds.RecycleApplicationPool &&
                e.DeploymentContext.IsDeploying)
            {
                e.DeploymentContext.Logger.WriteLine("The application pool is about to be " +
                    "recycled while the solution is being deployed.", LogCategory.Status);
            }
        }

        private void DeploymentStepCompleted(object sender, DeploymentStepCompletedEventArgs e)
        {
            if (e.DeploymentStepInfo.Id == DeploymentStepIds.RecycleApplicationPool &&
                e.DeploymentContext.IsRetracting)
            {
                e.DeploymentContext.Logger.WriteLine("The application pool was " +
                    "recycled while the solution is being retracted.", LogCategory.Status);
            }
        }
    }
}

コードのコンパイル

この例は、次のアセンブリへの参照を必要とします。

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

拡張機能の配置

拡張機能を配置するには、アセンブリと、拡張機能に同梱する必要のあるその他のファイルを提供するための Visual Studio Extension (VSIX) パッケージを作成します。 詳細については、「Visual Studio での SharePoint ツールの拡張機能の配置」を参照してください。

参照

その他の技術情報

SharePoint のパッケージ化と配置の拡張

チュートリアル: SharePoint プロジェクトに対するカスタムの配置手順の作成

方法: SharePoint プロジェクトの配置時または取り消し時にコードを実行する