Blog

TDS Custom Post Deploy Step

May 27, 2016 | Charles Turano

Team Development for Sitecore version 5.5 allows developers to add post deployment steps to to their deployments and update packages. Team Development for Sitecore has used post deployment steps internally to perform a number of useful functions. Many of the developers using TDS have requested the ability to add their own post deploy functionality. With the release of TDS 5.5 in early 2016, this functionality is now available.

TDS allows you to deploy your items using the TDS service or an update package. TDS will execute any post deployment steps you specify for both types of deployments.

Team Development for Sitecore comes with 3 out of the box post deployment steps:

  1. Trigger Save Event - TDS will trigger a save event for every deployed item. This is useful for items like Sitecore Campaigns, which need to update the Analytics database when they are deployed. These updates are usually done with a save event.
  2. Update Link Datbase - Sitecore serialization doesn't automatically update the link database when an item is de-serialized. Adding this post step to your deployments ensures that the link database is always up to date.
  3. Publish After Deploy - In some cases, it is beneficial to have deployed items automatically published. This post deployment step automatically performs that function.

While these functions are useful, it doesn't address the need to add your own custom steps.

Creating a custom step

Post deployment steps in Team Development for Sitecore was built to easily allow developers to add their own functionality to the deployment process. There are a few simple steps the developer needs to do to create a custom post deployment step.

Post Deployment Class

To create a post deployment class, the class needs to inherit from the IPostDeployAction interface. This interface is located in the Assembly HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.dll, which can be found in the TDS Nuget Package or in the folder C:\Program Files (x86)\MSBuild\HedgehogDevelopment\SitecoreProject\v9.0.

The interface is very simple, only containing a single method the developer must implement:

namespace HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.Contracts
{
    public interface IPostDeployAction
    {
        void RunPostDeployAction(XDocument deployedItems, 
                                 IPostDeployActionHost host, 
                                 string parameter);
    }
}

The method parameters are as follows:

  • deployedItems - An XML document that contains information about the items in the update package. This document contains information about the deployed items. See below for a sample deployedItems xml document.
  • host - The host parameter provides an interface used to write messages to the deployment log.
  • parameter - This is an optional string parameter the user can supply to the post step.

The deployedItems XML is generated at build time and is located in the /_Dev/DeployedItems.xml file in the update package. This contains some basic information about the items in the update pacakge:

<DeployedItems RecursiveDeployAction="Ignore">
  <DeployedItem Id="{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}" Name="content.item"
             Parent="{11111111-1111-1111-1111-111111111111}" Database="master" />
  <DeployedItem Id="{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}" Name="Home.item"
             Parent="{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}" Database="master" />
  <DeployedItem Id="{9992D85A-D251-491A-AB2F-C43BF0408B65}" Name="News.item"
             Parent="{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}" Database="master" />
</DeployedItems>   

The implementation of the interface IPostDeployAction can perform any operations the developer needs as part of their deployment.

Adding the class to the project

After creating the post deployment class, it needs to be added to the project so it can be included in the build. The easiest way to do this is to include the class in another assembly that is being deployed, or ensure the assembly is copied to the /bin folder of the Source Web Project referenced in the TDS General propert page.

The project file must be updated to contain a PostDeployAction item with the fully qualified class name of the post deployment class. At this time, this step must be done by manually editing the .scproj file. Below is an example of the PostDeployAction item for our example project:

<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
    <PostDeployAction Include="CustomPostBuildStep.LogItemNameAndLastUpdatedDate,CustomPostBuildStep">
        <Order>0</Order>
    </PostDeployAction>
</ItemGroup>

A simple example

The purpose of this example post deployment step is to write the last update time of each version of a deployed item to the deployment log. This isn't very useful, but it illustrates how post deployment steps are created.

The post deployment step class

The implementation of the post deployment step is quite simple:

[Description(description: "Logs the item path, and when each version of the item was last updated.")]
public class LogItemNameAndLastUpdatedDate : IPostDeployAction
{
    public void RunPostDeployAction(XDocument deployedItems, IPostDeployActionHost host, string parameter)
    {
        //Use the built in function to iterate over all deployed items
        PostDeployActionSupport.ExecuteOnAllDeployedItems(deployedItems, (deployedItemInPackage, deployedItemId, databaseName) =>
        {
            //Get the deployed item from the database
            Database db = Database.GetDatabase(databaseName);
            Item deployedItem = db.GetItem(new ID(deployedItemId));

            //Loop over the languages in the item
            foreach(Language lang in deployedItem.Languages)
            {
                Item deployItemForLanguage = db.GetItem(deployedItem.ID, lang);

                //Get the versions for the item
                foreach(Item versionedItem in deployItemForLanguage.Versions.GetVersions())
                {
                    //Write the update time of the version to the log
                    host.LogMessage(
                        "Item {0}, {1}, {2} was last updated on {3}", 
                        versionedItem.Paths.FullPath, lang.Name, 
                        versionedItem.Version, 
                        DateUtil.ParseDateTime(versionedItem["__Updated"], DateTime.MinValue));
                }
            }
        });
    }
}

The PostDeployActionSupport class is used to iterate over the the deployed items XElements in the deployedItems parameter. The class takes an XDocument and calls an action for each deployed item contained in the XDocument.

The rest of the implementation loads the item from the Sitecore database and logs the information using the host object passed to the method.

Deploy property page

Once the fully qualified class has been added to the .scproj file, the developer can manipulate the class in the deploy property page:

The [Description] attribute on the class allows the developer to provide some simple help text to the class. If you wish to have this text show up in the property page instead of the class name, copy the assembly containing the class to the folder where the HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.dll is located.

Wrapping it up


When the project is deployed using the service or an update package, the custom post deployment step will run and write messages to the log file. You can download the complete project here.