Share via


How to: Run code when deployment steps are executed

Applies to: yesVisual Studio noVisual 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 for a deployment step in a SharePoint project, you can handle events that are raised by SharePoint project items before and after Visual Studio executes each deployment step. For more information, see Extending SharePoint Packaging and Deployment.

To run code when deployment steps are executed

  1. Create a project item extension, a project extension, or a definition of a new project item type. For more information, see the following topics:

  2. In the extension, handle the DeploymentStepStarted and DeploymentStepCompleted events of an ISharePointProjectItemType object (in a project item extension or project extension) or an ISharePointProjectItemTypeDefinition object (in a definition of a new project item type).

  3. In the event handlers, use the DeploymentStepStartedEventArgs and DeploymentStepCompletedEventArgs parameters to get information about the deployment step. For example, you can determine which deployment step is executing and whether the solution is being deployed or retracted.

Example

The following code example demonstrates how to handle the DeploymentStepStarted and DeploymentStepCompleted events in an extension for the List Instance project item. This extension writes an additional message to the Output window when Visual Studio recycles the application pool while deploying and retracting the solution.

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);
            }
        }
    }
}

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.

See also