How to: Run Code When Deployment Steps are Executed

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.

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.

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

This example requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

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 Deploying Extensions for the SharePoint Tools in Visual Studio.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft