How to: Run Code When a SharePoint Project is Deployed or Retracted

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 Extending SharePoint Packaging and Deployment.

To run code when a SharePoint project is deployed or retracted

  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, access the ISharePointProjectService object. For more information, see How to: Retrieve the SharePoint Project Service.

  3. Handle the DeploymentStarted and DeploymentCompleted events of the project service.

  4. 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.

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

Compiling the Code

This example requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

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

See Also

Concepts

Extending SharePoint Packaging and Deployment

How to: Run Code When Deployment Steps are Executed