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.

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

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