如何:处理部署冲突

更新:2010 年 5 月

可以提供自己的代码以处理 SharePoint 项目项的部署冲突。 例如,您可以确定当前项目项中的任何文件是否已位于部署位置,然后在部署当前项目项之前删除已部署的文件。 有关部署冲突的更多信息,请参见扩展 SharePoint 打包和部署

处理部署冲突

  1. 创建项目项扩展、项目扩展或新项目项类型的定义。 有关更多信息,请参见下列主题:

  2. 在扩展中,处理 ISharePointProjectItemType 对象(在项目项扩展或项目扩展中)或 ISharePointProjectItemTypeDefinition 对象(在新项目项类型的定义中)的 DeploymentStepStarted 事件。

  3. 在事件处理程序中,根据适用于您的方案的标准来确定 SharePoint 网站上正在部署的项目项与已部署的解决方案之间是否发生冲突。 可以使用事件实参参数的 ProjectItem 属性来分析正在部署的项目项,并且可以通过调用为此定义的 SharePoint 命令来分析位于部署位置的文件。

    对于很多类型的冲突,您可能先要确定正在执行的部署步骤。 可以使用事件实参参数的 DeploymentStepInfo 属性来做到这一点。 虽然在内置 AddSolution 部署步骤中检测冲突通常会有用,但可以在任何部署步骤中检查冲突。

  4. 如果存在冲突,则使用事件实参参数的 Conflicts 属性的 Add 方法来创建新的 IDeploymentConflict 对象。 此对象表示部署冲突。 在调用 Add 方法时,还要指定解决冲突需调用的方法。

示例

下面的代码示例演示处理列表定义项目项的项目项扩展中的部署冲突的基本过程。 若要处理不同类型的项目项的部署冲突,请将不同的字符串传递到 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 扩展 (VSIX) 包。 有关更多信息,请参见在 Visual Studio 中部署 SharePoint 工具扩展

请参见

任务

如何:在执行部署步骤时运行代码

其他资源

扩展 SharePoint 打包和部署

扩展 SharePoint 项目项

如何:创建 SharePoint 命令

修订记录

日期

修订记录

原因

2010 年 5 月

新增主题。

信息补充。