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

如果您希望为 SharePoint 项目中的部署步骤执行其他任务,则可以在 Visual Studio 执行每个部署步骤的前后处理由 SharePoint 项目项引发的事件。 有关更多信息,请参见扩展 SharePoint 打包和部署

在执行部署步骤时运行代码

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

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

  3. 在事件处理程序中,使用 DeploymentStepStartedEventArgsDeploymentStepCompletedEventArgs 参数获取有关部署步骤的信息。 例如,您可以确定正在执行哪一个部署步骤以及是部署还是收回解决方案。

示例

下面的代码示例演示如何在“列表实例”项目项的扩展中处理 DeploymentStepStartedDeploymentStepCompleted 事件。 若 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 扩展 (VSIX) 包。 有关更多信息,请参见在 Visual Studio 中部署 SharePoint 工具扩展

请参见

任务

演练:为 SharePoint 项目创建自定义部署步骤

其他资源

扩展 SharePoint 打包和部署

如何:在部署或收回 SharePoint 项目时运行代码