Script enforcing mandatory values in all cultures
This web script example ensures that a multi-language property on an entity contains values for all required cultures. If any cultures are missing values, it throws a validation exception listing the missing cultures. This is useful for ensuring multi-language content is complete before saving or publishing an entity.
-
Ensure all assets referenced in the script exist in the Content Hub schema.
-
Define any required properties used in the script logic, such as
TitleorDescription. -
Verify that users or roles executing the script have the necessary permissions to create or modify the referenced entities.
Script
using System.Linq;
using System.Collections.Generic;
using Stylelabs.M.Sdk.Models.Base;
var entity = Context.Target as IEntity;
if (entity == null)
{
return;
}
// loading the multi-language property in all cultures
var loadConfiguration = new EntityLoadConfiguration(
CultureLoadOption.All,
new PropertyLoadOption(new List<string> { "YourMultiLanguagePropertyName" }),
RelationLoadOption.None);
entity = await MClient.Entities.GetAsync(entity.Id.Value, loadConfiguration);
if (entity.GetProperty("YourMultiLanguagePropertyName") is CultureSensitiveProperty property)
{
// checking if any values are missing
// if so collecting the cultures' names
var missingRequiredValues =
property.GetValues<string>()
.Where(x => string.IsNullOrEmpty(x.Value))
.Select(x => x.Key.Name)
.ToArray();
if (missingRequiredValues.Any())
{
var validationFailures = new List<ValidationFailure>
{
new ValidationFailure("YourMultiLanguagePropertyName",
"Missing required values for cultures: " + string.Join(", ", missingRequiredValues))
};
// throwing a validation exception with information on the missing values
throw new ValidationException("Missing required values", validationFailures);
}
}Script explanation
This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.
var entity = Context.Target as IEntity;
if (entity == null)
{
return;
}if (missingRequiredValues.Any())
{
var validationFailures = new List<ValidationFailure>
{
new ("Missing required values for cultures: " + string.Join(", ", missingRequiredValues), "YourMultiLanguagePropertyName")
};
-
Import essential C# libraries to handle collections, queries, and asynchronous operations.
RequestResponseusing System.Linq; using System.Collections.Generic; using Stylelabs.M.Sdk.Models.Base; -
Retrieve the target entity from the
Context.Target. If the entity isnull(not valid), the script exits immediately to prevent further processing. -
Define a load configuration to retrieve the specified multi-language property across all cultures. This ensures the script has access to all language variations.
RequestResponsevar loadConfiguration = new EntityLoadConfiguration( CultureLoadOption.All, new PropertyLoadOption(new List<string> { "YourMultiLanguagePropertyName" }), RelationLoadOption.None); -
Reload the entity using the defined configuration, ensuring the required property is properly loaded.
RequestResponseentity = await MClient.Entities.GetAsync(entity.Id.Value, loadConfiguration); -
Check whether the entity contains a culture-sensitive property named
"Description", ensuring that the property supports multiple languages.RequestResponseif (entity.GetProperty("YourMultiLanguagePropertyName") is CultureSensitiveProperty property) -
Retrieve all existing values for the property and identifies missing values. If a value is empty (
nullor an empty string), the corresponding culture name is added to a list.RequestResponsevar missingRequiredValues = property.GetValues<string>() .Where(x => string.IsNullOrEmpty(x.Value)) .Select(x => x.Key.Name) .ToArray(); -
Check all values, if any are missing, the script generate an error message that includes the list of cultures with missing values. If no values were missing, no further action is taken and the script exits.
After creating the error message, the script throws a validation exception, preventing the entity from being saved until all missing values are provided.
throw new ValidationException("Missing required values", validationFailures);
}
Setup
-
Create, publish, and enable an Action script.
-
Create an action of type Action script and link it with the script.
-
Create a new trigger and set its objective to Entity creation and Entity modification.
-
In the trigger conditions, add the entity definition Asset.
-
In the trigger actions, add the new action under Validation actions.
-
-
Save and activate the trigger.