1. Scripts

Action script with a pre-commit trigger

This example shows how to automatically validate or modify entity data before it's saved, ensuring that all required fields are complete and compliant with your business rules. The web script is connected to a pre-commit trigger that executes before applied changes are saved by the system. This is useful, for example, to ensure images and web-friendly file types are appropriately categorized without manual intervention when uploading assets in bulk.

Before you begin
  • Ensure the assets referenced in the script exist in the Content Hub schema.

  • Define any required properties, such as Title or Description, used in the script logic.

  • Verify that users or roles executing the script have the necessary permissions to create or modify the referenced entities.

Script

using System.Linq;

var webExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif" };

var entity = Context.Target as IEntity;

// Make sure that the following members are loaded
await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));

var filename = entity.GetPropertyValue<string>("FileName");
if (string.IsNullOrEmpty(filename)) return; // No filename, nothing to do.

var extension = GetExtension(filename)?.ToLowerInvariant();

if (string.IsNullOrEmpty(extension)) return;
if (!webExtensions.Contains(extension)) return;

var query = Query.CreateQuery(entities => 
  from e in entities
  where e.Identifier == "M.AssetType.Web"
  select e);

var webTypeId = await MClient.Querying.SingleIdAsync(query);
if (!webTypeId.HasValue) return;

var relation = entity.GetRelation<IChildToOneParentRelation>("AssetTypeToAsset");
if (relation == null) return;

relation.Parent = webTypeId;



string GetExtension(string path)
{
  var tokens = path.Split('.');
  if (tokens.Length > 1)
  {
    return "." + tokens[tokens.Length - 1];
  }
  return null;
}

Script explanation

This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.

  1. Include the libraries to be used in the script. To understand which libraries to include, see Context properties (property types).

    using System.Linq;
  2. Define a list of web-compatible file extensions to determine which files qualify as web assets.

    var webExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif" };
  3. Make sure that the file name property and asset type relation are loaded. If they are not, they will be lazy loaded.

    // Make sure that the following members are loaded
    await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));
  4. Retrieve the target entity from the context (Context.Target) and loaded with the necessary members. These include the FileName property and the AssetTypeToAsset relation. If the entity is invalid or the filename is empty, the script exits.

    var entity = Context.Target as IEntity;
    
    // Make sure that the following members are loaded
    await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));
    
    var filename = entity.GetPropertyValue<string>("FileName");
    if (string.IsNullOrEmpty(filename)) return;
    Warning

    The Target object type depends on the trigger's objectives (for example, entity, entity definition, or policy). In this case, the trigger's objectives are Entity creation and Entity modification. Hence, we need to cast Target to IEntity.

  5. Extract the file extension from the filename using the GetExtension helper method. If the extension is missing or not in the list of web-compatible extensions, the script exits.

    var extension = GetExtension(filename)?.ToLowerInvariant();
    
    if (string.IsNullOrEmpty(extension)) return;
    if (!webExtensions.Contains(extension)) return;
  6. Create a query to find the M.AssetType.Web entity. If the entity is not found, the script exits.

    var query = Query.CreateQuery(entities => 
      from e in entities
      where e.Identifier == "M.AssetType.Web"
      select e);
    
    var webTypeId = await MClient.Querying.SingleIdAsync(query);
    if (!webTypeId.HasValue) return;
    Note

    The MClient object is always available and can be used by all script types.

  7. Retrieve the AssetTypeToAsset relation from the asset. If the relation exists, it sets the parent of the relation to the M.AssetType.Web entity, linking the asset to the web asset type. If there is no relation, the script exits.

    var relation = entity.GetRelation<IChildToOneParentRelation>("AssetTypeToAsset");
    if (relation == null) return;
    
    relation.Parent = webTypeId;
  8. The helper method GetExtension extracts the file extension from the filename, ensuring that only relevant file types are processed.

    string GetExtension(string path)
    {
      var tokens = path.Split('.');
      if (tokens.Length > 1)
      {
        return "." + tokens[tokens.Length - 1];
      }
      return null;
    }

Setup

  1. On the Taxonomy page, create an asset type named Web with the identifier M.AssetType.Web.

  2. Create, publish, and enable the script above as an Action script.

  3. Create an action of type Action script and link it to the script.

  4. Create a new trigger and set the trigger's objectives to Entity creation and Entity modification.

    • In the trigger conditions, add the entity definition Asset then add the condition Filename has changed.

    • In the trigger actions, add the new action to Pre-commit actions.

  5. Save and activate the trigger.

If you have suggestions for improving this article, let us know!