如何:在部署或收回 SharePoint 项目时运行代码
如果要在部署或收回 SharePoint 项目时执行其他任务,您可以处理 Visual Studio 引发的事件。 有关更多信息,请参见扩展 SharePoint 打包和部署。
在部署或收回 SharePoint 项目时运行代码
创建项目项扩展、项目扩展或新项目项类型的定义。 有关更多信息,请参见下列主题:
在扩展中,访问 ISharePointProjectService 对象。 有关更多信息,请参见如何:检索 SharePoint 项目服务。
处理项目服务的 DeploymentStarted 和 DeploymentCompleted 事件。
在事件处理程序中,使用 DeploymentEventArgs 参数来获取有关当前部署会话的信息。 例如,您可以确定哪个项目处于当前部署会话中,以及是否正在部署或收回该项目。
下面的代码示例演示如何在项目扩展中处理 DeploymentStarted 和 DeploymentCompleted 事件。 当 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 工具扩展。