共用方式為


HOW TO:處理部署衝突

您可以自行提供程式碼,處理 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 命令的詳細資訊,請參閱 HOW TO:建立 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 擴充功能 (VSIX) 套件。如需詳細資訊,請參閱部署 Visual Studio 中 SharePoint 工具的擴充功能

請參閱

概念

擴充 SharePoint 封裝和部署

擴充 SharePoint 專案項目

HOW TO:在執行部署步驟時執行程式碼

HOW TO:建立 SharePoint 命令