How to: Run code when a SharePoint project is deployed or retracted
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
If you want to perform additional tasks when a SharePoint project is deployed or retracted, you can handle events that are raised by Visual Studio. For more information, see Extend SharePoint packaging and deployment.
To run code when a SharePoint project is deployed or retracted
Create a project item extension, a project extension, or a definition of a new project item type. For more information, see the following topics:
In the extension, access the ISharePointProjectService object. For more information, see How to: Retrieve the SharePoint project service.
Handle the DeploymentStarted and DeploymentCompleted events of the project service.
In the event handlers, use the DeploymentEventArgs parameter to get information about the current deployment session. For example, you can determine which project is in the current deployment session and whether it is being deployed or retracted.
The following code example demonstrates how to handle the DeploymentStarted and DeploymentCompleted events in a project extension. This extension writes an additional message to the Output window when deployment starts and completes for a SharePoint project.
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); } } } }
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
Compile the code
This example requires references to the following assemblies:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
Deploy the extension
To deploy the extension, create a Visual Studio extension (VSIX) package for the assembly and any other files that you want to distribute with the extension. For more information, see Deploy extensions for the SharePoint tools in Visual Studio.