共用方式為


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

如果您要針對 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 封裝和部署

HOW TO:在 SharePoint 專案部署或撤銷時執行程式碼