Mike Robbins

Sitecore Developer Blog

Follow me on GitHub

Publish And Send For Translate Workflow Action

Here is a useful Custom Workflow action I've written allowing a content editor to publish a content item to live while also sending its alternative language version to a translate workflow state. This allows a content item to be published to live in a single language without being held up waiting on a translation to be completed.

  • 1. Create a workflow command to publish the content to the published state in workflow.
  • 2. Create a new custom workflow action underneath the publish command.
  • 3. Use the code below to automatically create a new language version and set the workflow state to translation state.
  • 4. Replace the parameters in the screenshot of the custom workflow action. Replace the targetstate parameter with the ID of the target language state. Replace language with the name of the target language

Publish And Translate Action

using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Web;
using Sitecore.Workflows.Simple;
namespace MikeRobbins.SitecoreComponents.WorkFlow
{
    public class PublishAction<br />
    {
        public void Process(WorkflowPipelineArgs args)
        {
            Item workFlowItem = args.DataItem;
            var targetLanguage = GetTargetLanguage(args.ProcessorItem.InnerItem);
            var targetVersion = Sitecore.Context.ContentDatabase.GetItem(workFlowItem.ID, targetLanguage);
            if (targetVersion.Versions.Count == 0)
            {
                targetVersion = targetVersion.Versions.AddVersion();
            }
            var targetWorkflowState = GetWorkflowState(args.ProcessorItem.InnerItem);
            SetWorkflowState(targetVersion, targetWorkflowState.ID);
       }

        private static void SetWorkflowState(Item welshVersion, ID targetWorkflowState)
        {
            welshVersion.Editing.BeginEdit();
            welshVersion.Fields[FieldIDs.WorkflowState].Value = targetWorkflowState.ToString();
            welshVersion.Editing.EndEdit();
       }

        private static Item GetWorkflowState(Item actionItem)
        {
            ID translateId = null;
            return ID.TryParse(WebUtil.ParseUrlParameters(actionItem["parameters"])["targetstate"], out translateId) ? Sitecore.Context.ContentDatabase.GetItem(translateId) : null;
        }
        
        public Language GetTargetLanguage(Item actionItem)
        {
            var language = WebUtil.ParseUrlParameters(actionItem["parameters"])["language"];
            Assert.IsNotNullOrEmpty(language, "Language needs to be specified as a parameter (&amp;language=cy-gb)");
            return LanguageManager.GetLanguage(language);
        }
    }
}