Share via


How to: Handle Deployment Conflicts

You can provide your own code to handle deployment conflicts for a SharePoint project item. For example, you might determine whether any files in the current project item already exist in the deployment location, and then delete the deployed files before the current project item is deployed. For more information about deployment conflicts, see Extending SharePoint Packaging and Deployment.

To handle a deployment conflict

  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 event 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 handler, determine whether there is a conflict between the project item that is being deployed and the deployed solution on the SharePoint site, based on criteria that apply to your scenario. You can use the ProjectItem property of the event arguments parameter to analyze the project item that is being deployed, and you can analyze the files at the deployment location by calling a SharePoint command that you define for this purpose.

    For many types of conflicts, you might first want to determine which deployment step is executing. You can do this by using the DeploymentStepInfo property of the event arguments parameter. Although it typically makes sense to detect conflicts during the built-in AddSolution deployment step, you can check for conflicts during any deployment step.

  4. If a conflict exists, use the [M:Microsoft.VisualStudio.SharePoint.Deployment.IDeploymentConflictCollection.Add(System.String,System.Func`2,System.Boolean)] method of the Conflicts property of the event arguments to create a new IDeploymentConflict object. This object represents the deployment conflict. In your call to the [M:Microsoft.VisualStudio.SharePoint.Deployment.IDeploymentConflictCollection.Add(System.String,System.Func`2,System.Boolean)] method, also specify the method that is called to resolve the conflict.

Example

The following code example demonstrates the basic process for handling a deployment conflict in a project item extension for list definition project items. To handle a deployment conflict for a different type of project item, pass a different string to the SharePointProjectItemTypeAttribute. For more information, see Extending SharePoint Project Items.

For simplicity, the DeploymentStepStarted event handler in this example assumes that a deployment conflict exists (that is, it always adds a new IDeploymentConflict object), and the Resolve method simply returns true to indicate that the conflict was resolved. In a real scenario, your DeploymentStepStarted event handler would first determine if a conflict exists between a file in the current project item and a file at the deployment location, and then add an IDeploymentConflict object only if a conflict exists. For example, you might use the e.ProjectItem.Files property in the event handler to analyze the files in the project item, and you might call a SharePoint command to analyze the files at the deployment location. Similarly, in a real scenario the Resolve method might call a SharePoint command to resolve the conflict on the SharePoint site. For more information about creating SharePoint commands, see How to: Create a SharePoint Command.

Imports Microsoft.VisualStudio.SharePoint
Imports Microsoft.VisualStudio.SharePoint.Deployment
Imports System.ComponentModel.Composition

<Export(GetType(ISharePointProjectItemTypeExtension))>
<SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListDefinition")>
Public Class DeploymentConflictExtension
    Implements ISharePointProjectItemTypeExtension

    Private Sub Initialize(ByVal projectItemType As ISharePointProjectItemType) _
    Implements ISharePointProjectItemTypeExtension.Initialize
        AddHandler projectItemType.DeploymentStepStarted, AddressOf DeploymentStepStarted
    End Sub 

    Private Sub DeploymentStepStarted(ByVal Sender As Object, ByVal e As DeploymentStepStartedEventArgs)
        If e.DeploymentStepInfo.Id = DeploymentStepIds.AddSolution Then
            e.Conflicts.Add("This is an example conflict", AddressOf Me.Resolve, True)
            e.ProjectItem.Project.ProjectService.Logger.WriteLine("Added new example conflict.", LogCategory.Status)
        End If 
    End Sub 

    Private Function Resolve(ByVal projectItem As ISharePointProjectItem) As Boolean
        projectItem.Project.ProjectService.Logger.WriteLine("Returning 'true' from Resolve method for example conflict.",
            LogCategory.Status)
        Return True 
    End Function 
End Class
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Deployment;
using System.ComponentModel.Composition;

namespace Contoso.DeploymentConflictExtension
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListDefinition")]
    class DeploymentConflictExtension : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.DeploymentStepStarted += DeploymentStepStarted;
        }

        private void DeploymentStepStarted(object sender, DeploymentStepStartedEventArgs e)
        {
            if (e.DeploymentStepInfo.Id == DeploymentStepIds.AddSolution)
            {
                e.Conflicts.Add("This is an example conflict", this.Resolve, true);
                e.ProjectItem.Project.ProjectService.Logger.WriteLine("Added new example conflict.", LogCategory.Status);
            }
        }

        private bool Resolve(ISharePointProjectItem projectItem)
        {
            projectItem.Project.ProjectService.Logger.WriteLine("Returning 'true' from Resolve method for example conflict.", 
                LogCategory.Status);
            return true;
        }
    }
}

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

Extending SharePoint Project Items

How to: Run Code When Deployment Steps are Executed

How to: Create a SharePoint Command