Using Custom Logic to Remove Items from the Translation Queue

You can call the <RemoveFromTranslationQueuePipeLine> pipeline to remove Sitecore items from the Translation Queue. The items are displayed in the queue only for the Context.User.

Prerequisite:

You must set the Context.User to a user with Sitecore access. This must be a user with permission to review and send out the queued items. If you do not set the context user, then the default user is sitecore\Anonymous, which prevents the items from being displayed in the queue.

Identifying the content items to remove

Since the same item may have been added multiple times to the Translation Queue (particularly from the Bulk Translation wizard), the Sitecore item ID (SitecoreID) may identify multiple content items to remove, instead of identifying a single content item to remove.

Recommendation: Use the SourceLanguage, TargetLanguages, or SourceVersion parameters to identify which items with the same Sitecore item ID to remove.

  • If the SourceLanguage or TargetLanguages are null, the pipeline removes items with any value, which means it removes items in all source or target languages.
  • If the SourceVersion is 0 (the default value), the pipeline removes items with any value, which means it removes items for all source versions.
To call the pipeline:
  1. Create an instance of the ClayTablet.SC.Pipelines.RemoveFromTranslationQueuePipeLineArgs class to pass to the pipeline as argument.

The RemoveFromTranslationQueuePipelineArgs class is defined in ClayTablet.SC.dll as:

public class RemoveFromTranslationQueuePipelineArgs : PipelineArgs

{

public List&lt;ItemToRemove&gt; ItemsToRemove { get; set; } = new List&lt;ItemToRemove&gt;();

public int RemovedItems { get; internal set; } = 0;

public int Errors { get; internal set; } = 0;

}

  1. In ItemsToRemove, specify the list of items to remove from the Translation Queue.

The ItemToRemove class is defined as:

public class ItemToRemove

{

public String ID { get; set; }

public String SourceLanguage { get; set; }

public String TargetLanguages { get; set; }

public int SourceVersion { get; set; }

public String ErrorMessage { get; internal set; }

public List&lt;ItemDetails&gt; ItemsDetails { get; internal set; } = new List&lt;ItemDetails&gt;();

public int RemovedItems { get; internal set; } = 0;

public bool HasError { get; internal set; } = false;

}

Parameter Description
SourceLanguage The language code of the source language of the item to remove from the queue. For example, to remove an item whose source language is English, specify en.

Note: If this is null (blank), the pipeline will remove items in all source languages.
TargetLanguages The language code of the target language(s) of the item(s) to remove from the queue. For example:

* To remove an item whose target language is French, specify ‘fr-FR’.
* To remove an item whose target language is French, and an item whose target language is German, specify ‘fr-FR,de-DE’. Use a comma ( , ) as a separator of multiple language codes.

Note: If this is null (blank), the pipeline will remove items in all target languages.
SourceVersion The source-language version of the item(s) to remove from the queue. For example, to remove an item whose source-language version is 1, specify 1.

Note: If this is 0 (the default value), the pipeline will remove all source-language versions of the item.
ErrorMessage Information about any error that occurs when removing items from the queue.
ItemsDetails A detailed list of items removed from the queue.
RemovedItems The number of removed items from the queue with the specified Sitecore ID.
HasError A Boolean value indicating whether an error occurred.
ItemsDetails model with information about removed items, as described below.

The ItemDetails class is defined as:

public class ItemDetails

{

public String SitecoreID { get; internal set; }

public String DatabaseID { get; internal set; }

public String SourceLanguage { get; internal set; }

public String TargetLanguages { get; internal set; }

public String ItemDeadLine { get; internal set; }

public String SiteCoreUser { get; internal set; }

public DateTime CreateTime { get; internal set; }

public int Version { get; internal set; }

public bool IsRemoved { get; internal set; } = false;

}

  1. Invoke the pipeline to remove the specified Sitecore items from the Translation Queue.

  2. Use the IsRemoved property to check how many Sitecore items were removed from the Translation Queue.

The following is sample code for calling the pipeline:

private void RemoveFromQueue(string itemId, string sourceLanguage = null, string targetLanguage = null, int sourceVersion = 0)

{

// Create list of objects to remove.

var args = new RemoveFromTranslationQueuePipelineArgs();

args.ItemsToRemove.Add(new ItemToRemove()

{

    ID = itemId,

    SourceLanguage = sourceLanguage,

    TargetLanguages = targetLanguage,

    SourceVersion = sourceVersion

});

// Invoke pipeline.

CorePipeline.Run("RemoveFromTranslationQueuePipeLine", args);

// Display total number of removed items.

Console.WriteLine($"Removed {args.RemovedItems} items and {args.Errors} errors occurred");

// Get removed items.

var removed = args.ItemsToRemove.First();

// Display any error messages.

if (removed.HasError)

Console.WriteLine($"Error message: {removed.ErrorMessage}");

// Display details about a removed item.

foreach(ItemDetails item in removed.ItemsDetails)

{

    if (item.IsRemoved)

    Console.WriteLine($"Removed translate item from source language: {item.SourceLanguage} to target language: {item.TargetLanguages}");

    else

    Console.WriteLine($"Can not removed translate item from source language: {item.SourceLanguage} to target language: {item.TargetLanguages}");

}

}