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
Script explanation
This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.
-
Import essential C# libraries to handle collections, queries, and asynchronous operations.
-
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.
-
Reload the entity using the defined configuration, ensuring the required property is properly loaded.
-
Check whether the entity contains a culture-sensitive property named
"Description", ensuring that the property supports multiple languages. -
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. -
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.
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.