共用方式為


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

若您要在部署或撤銷 SharePoint 專案時執行額外的工作,可以處理 Visual Studio 所引發的事件。 如需詳細資訊,請參閱擴充 SharePoint 封裝和部署

若要在部署或撤銷 SharePoint 專案時執行程式碼

  1. 建立一個專案項目擴充功能、專案擴充功能或新專案項目類型的定義。 如需詳細資訊,請參閱下列主題:

  2. 在擴充功能中,存取 ISharePointProjectService 物件。 如需詳細資訊,請參閱 HOW TO:擷取 SharePoint 專案服務

  3. 處理專案服務的 DeploymentStartedDeploymentCompleted 事件。

  4. 在事件處理常式中,使用 DeploymentEventArgs 參數取得目前部署工作階段的相關資訊。 例如,您可以判斷哪一個專案處於目前部署工作階段中,以及是否正在部署或撤銷專案。

下列程式碼範例示範如何處理專案擴充功能中的 DeploymentStartedDeploymentCompleted 事件。 當啟動和完成 SharePoint 專案的部署時,此擴充功能會將額外的訊息寫入 [輸出] 視窗。

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

Namespace Contoso.ProjectDeploymentExtension

    <Export(GetType(ISharePointProjectExtension))> _
    Friend Class ExampleProjectDeploymentExtension
        Implements ISharePointProjectExtension

        Private Sub Initialize(ByVal projectService As ISharePointProjectService) _
            Implements ISharePointProjectExtension.Initialize
            AddHandler projectService.DeploymentStarted, AddressOf DeploymentStarted
            AddHandler projectService.DeploymentCompleted, AddressOf DeploymentCompleted
        End Sub

        Private Sub DeploymentStarted(ByVal Sender As Object, ByVal e As DeploymentEventArgs)
            If e.DeploymentContext.IsDeploying Then
                Dim message As String = String.Format("Deployment started for the {0} project.", 
                     e.Project.Name)
                e.DeploymentContext.Logger.WriteLine(message, LogCategory.Status)
            End If
        End Sub

        Private Sub DeploymentCompleted(ByVal Sender As Object, ByVal e As DeploymentEventArgs)
            If e.DeploymentContext.IsDeploying Then
                Dim message As String = String.Format("Deployment completed for the {0} project.", 
                    e.Project.Name)
                e.DeploymentContext.Logger.WriteLine(message, 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.ProjectDeploymentExtension
{
    [Export(typeof(ISharePointProjectExtension))]
    internal class ExampleProjectDeploymentExtension : ISharePointProjectExtension
    {
        public void Initialize(ISharePointProjectService projectService)
        {
            projectService.DeploymentStarted += ProjectService_DeploymentStarted;
            projectService.DeploymentCompleted += ProjectService_DeploymentCompleted;
        }

        void ProjectService_DeploymentStarted(object sender, DeploymentEventArgs e)
        {
            if (e.DeploymentContext.IsDeploying)
            {
                string message = String.Format("Deployment started for the {0} project.",
                     e.Project.Name);
                e.DeploymentContext.Logger.WriteLine(message, LogCategory.Status);
            }
        }

        void ProjectService_DeploymentCompleted(object sender, DeploymentEventArgs e)
        {
            if (e.DeploymentContext.IsDeploying)
            {
                string message = String.Format("Deployment completed for the {0} project.",
                     e.Project.Name);
                e.DeploymentContext.Logger.WriteLine(message, LogCategory.Status);
            }
        }
    }
}

編譯程式碼

這個範例需要參考下列組件:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

部署擴充功能

若要部署擴充功能,請針對組件以及要與擴充功能一起散發的任何其他檔案建立 Visual Studio 擴充功能 (VSIX) 套件。 如需詳細資訊,請參閱部署 Visual Studio 中 SharePoint 工具的擴充功能

請參閱

工作

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

其他資源

擴充 SharePoint 封裝和部署