---
title: "Script enforcing mandatory values in all cultures"
slug: "script-enforcing-mandatory-values-in-all-cultures"
abstract: "Web script example to ensure that multi-language properties have values in all required cultures before saving an entity."
---
# 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.

> [!note]
> **Before you begin**
> -   Ensure all assets referenced in the script exist in the Content Hub schema.
>
> -   Define any required properties used in the script logic, such as `Title` or `Description`.
>
> -   Verify that users or roles executing the script have the necessary permissions to create or modify the referenced entities.

## Script

```csharp
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.

```csharp
var entity = Context.Target as IEntity;
if (entity == null)
{
    return;
}

```

```csharp
if (missingRequiredValues.Any())
{
    var validationFailures = new List<ValidationFailure>
    {
        new ("Missing required values for cultures: " + string.Join(", ", missingRequiredValues), "YourMultiLanguagePropertyName")
    };

```

1. Import essential C# libraries to handle collections, queries, and asynchronous operations.

    ```csharp
    using System.Linq;
    using System.Collections.Generic;
    using Stylelabs.M.Sdk.Models.Base;
    ```

2. Retrieve the target entity from the `Context.Target`. If the entity is `null` (not valid), the script exits immediately to prevent further processing.
3. Define a load configuration to retrieve the specified multi-language property across all cultures. This ensures the script has access to all language variations.

    ```csharp
    var loadConfiguration = new EntityLoadConfiguration(
        CultureLoadOption.All,
        new PropertyLoadOption(new List<string> { "YourMultiLanguagePropertyName" }),
        RelationLoadOption.None);
    ```

4. Reload the entity using the defined configuration, ensuring the required property is properly loaded.

    ```csharp
    entity = await MClient.Entities.GetAsync(entity.Id.Value, loadConfiguration);
    ```

5. Check whether the entity contains a culture-sensitive property named `"Description"`, ensuring that the property supports multiple languages.

    ```csharp
    if (entity.GetProperty("YourMultiLanguagePropertyName") is CultureSensitiveProperty property)
    ```

6. Retrieve all existing values for the property and identifies missing values. If a value is empty (`null` or an empty string), the corresponding culture name is added to a list.

    ```csharp
    var missingRequiredValues =
        property.GetValues<string>()
            .Where(x => string.IsNullOrEmpty(x.Value))
            .Select(x => x.Key.Name)
            .ToArray();
    ```

7. 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.

```csharp
throw new ValidationException("Missing required values", validationFailures);
}

```

## Setup

1. [Create](https://doc.sitecore.com/ch/en/developers/cloud-dev/work-with-scripts.html "Work with scripts"), publish, and enable an *Action* script.
2. [Create an action](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-an-action.html "Create an action") of type *Action script* and link it with the script.
3. [Create a new trigger](https://doc.sitecore.com/ch/en/developers/cloud-dev/create-a-trigger.html "Create a 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**.
4. Save and activate the trigger.