方法: 配置競合を処理する

独自のコードを使用して SharePoint プロジェクト項目の配置競合を処理できます。たとえば、現在のプロジェクト項目に含まれるファイルが配置場所に既に存在するかどうかを判断し、現在のプロジェクトの項目を配置する前に、配置されているファイルを削除します。配置競合の詳細については、「SharePoint のパッケージ化と配置の拡張」を参照してください。

配置競合を処理するには

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

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

  3. イベント ハンドラーで、シナリオに適用される条件に基づいて、配置しようとしているプロジェクト項目と SharePoint サイトに配置されているソリューションの間で競合が発生しているかどうかを判断します。イベント引数パラメーターの ProjectItem プロパティを使用して、配置しようとしているプロジェクト項目を分析できます。また、このために定義する SharePoint コマンドを呼び出して、配置場所にあるファイルを分析できます。

    多くの種類の競合では、最初にどの配置手順が実行されているかを判断する必要があります。これは、イベント引数パラメーターの DeploymentStepInfo プロパティを使用して行うことができます。一般的には、組み込みの AddSolution 配置手順中に競合を検出することが理にかなっていますが、いずれの配置手順中でも競合を確認できます。

  4. 競合が存在する場合は、イベント引数の Conflicts プロパティの [M:Microsoft.VisualStudio.SharePoint.Deployment.IDeploymentConflictCollection.Add(System.String,System.Func`2,System.Boolean)] メソッドを使用して、新しい IDeploymentConflict オブジェクトを作成します。このオブジェクトは配置競合を表します。[M:Microsoft.VisualStudio.SharePoint.Deployment.IDeploymentConflictCollection.Add(System.String,System.Func`2,System.Boolean)] メソッドの呼び出しで、競合を解決するために呼び出すメソッドも指定します。

次のコード例は、リスト定義プロジェクト項目のプロジェクト項目の拡張機能における配置競合を処理する基本的なプロセスを示しています。別の種類のプロジェクト項目の配置競合を処理するには、別の文字列を SharePointProjectItemTypeAttribute に渡します。詳細については、「SharePoint プロジェクト項目の拡張」を参照してください。

説明を簡単にするため、この例の DeploymentStepStarted イベント ハンドラーでは、配置競合が存在することを前提としており (つまり、常に新しい IDeploymentConflict オブジェクトを追加する)、Resolve メソッドは競合が解決されたことを示す true だけを返します。実際のシナリオでは、DeploymentStepStarted イベント ハンドラーは、最初に現在のプロジェクト項目に含まれるファイルと配置場所にあるファイルの間で競合が存在しているかどうかを判断し、競合が存在する場合にのみ IDeploymentConflict オブジェクトを追加します。たとえば、イベント ハンドラーで e.ProjectItem.Files プロパティを使用してプロジェクト項目に含まれるファイルを分析し、SharePoint コマンドを呼び出して配置場所にあるファイルを分析します。同様に、実際のシナリオでは、Resolve メソッドが SharePoint コマンドを呼び出して SharePoint サイト上での競合を解決します。SharePoint コマンドの作成の詳細については、「方法: SharePoint コマンドを作成する」を参照してください。

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

<Export(GetType(ISharePointProjectItemTypeExtension))>
<SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListDefinition")>
Public Class DeploymentConflictExtension
    Implements ISharePointProjectItemTypeExtension

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

    Private Sub DeploymentStepStarted(ByVal Sender As Object, ByVal e As DeploymentStepStartedEventArgs)
        If e.DeploymentStepInfo.Id = DeploymentStepIds.AddSolution Then
            e.Conflicts.Add("This is an example conflict", AddressOf Me.Resolve, True)
            e.ProjectItem.Project.ProjectService.Logger.WriteLine("Added new example conflict.", LogCategory.Status)
        End If
    End Sub

    Private Function Resolve(ByVal projectItem As ISharePointProjectItem) As Boolean
        projectItem.Project.ProjectService.Logger.WriteLine("Returning 'true' from Resolve method for example conflict.",
            LogCategory.Status)
        Return True
    End Function
End Class
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Deployment;
using System.ComponentModel.Composition;

namespace Contoso.DeploymentConflictExtension
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListDefinition")]
    class DeploymentConflictExtension : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.DeploymentStepStarted += DeploymentStepStarted;
        }

        private void DeploymentStepStarted(object sender, DeploymentStepStartedEventArgs e)
        {
            if (e.DeploymentStepInfo.Id == DeploymentStepIds.AddSolution)
            {
                e.Conflicts.Add("This is an example conflict", this.Resolve, true);
                e.ProjectItem.Project.ProjectService.Logger.WriteLine("Added new example conflict.", LogCategory.Status);
            }
        }

        private bool Resolve(ISharePointProjectItem projectItem)
        {
            projectItem.Project.ProjectService.Logger.WriteLine("Returning 'true' from Resolve method for example conflict.", 
                LogCategory.Status);
            return true;
        }
    }
}

コードのコンパイル

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

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

拡張機能の配置

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

参照

概念

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

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

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

方法: SharePoint コマンドを作成する