logo
logo

Action script example (Pre-commit phase)

This is an example of an Action script to be executed by a Trigger in the Pre-commit phase (In process) when an asset is created or modified. It populates a default asset type (Web) for assets with specific extensions in their filenames.

Use case

  1. A user creates or modifies an asset.
  2. If the asset's filename has a specific extension, the asset's AssetType is set to Web.

Prerequisites

Web is not a default type in M.AssetType. It must be created, if required.

  • Navigate to Manage and click on Taxonomy.

  • Search for M.Asset:

  • To add a new item to the M.AssetType taxonomy, click +:

  • Enter Web as the label for the new item:

  • Click Save.

Script

using System.Linq;

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

var entity = Context.Target as IEntity;

// Ensure 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

  1. Include the libraries to be used in the script.

    using System.Linq;
    Tip

    To know which libraries to include, please refer to the Context properties section (property types).

  2. Define the list of extensions. If an asset's filename has one of these extensions, it will be marked with the AssetType Web.

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

    // Ensure the following members are loaded
    await entity.LoadMembersAsync(new PropertyLoadOption("FileName"), new RelationLoadOption("AssetTypeToAsset"));
  4. Retrieve the Target object from the Context and cast it to IEntity. Target is the asset involved in the creation or modification event.

    var entity = Context.Target as IEntity;
    Warning

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

    Tip

    The context property TargetType indicates the type of Target. The possible values of TargetType are Entity, EntityDefinition, Policy and DataSource. For more information about context properties, please refer to the Context properties section.

  5. Retrieve the Filename property from the Target object using GetPropertyValue.

    var filename = entity.GetPropertyValue<string>("FileName");
  6. If the asset does not have a filename, the script exits.

    if (string.IsNullOrEmpty(filename)) return;
  7. Get the extension from the filename using Path.GetExtension (System.IO).

    var extension = Path.GetExtension(filename)?.ToLowerInvariant();
  8. If the filename does not have an extension, the script exits.

    if (string.IsNullOrEmpty(extension)) return;
  9. If the filename has an extension, but it is not one of the specified extensions, the script exits.

    if (!webExtensions.Contains(extension)) return;
  10. Create a query that retrieves the ID of the AssetType Web with identifier M.AssetType.Web.

    var query = Query.CreateQuery(entities =>
        from e in entities
        where e.Identifier == "M.AssetType.Web"
        select e);
  11. Use the query to retrieve The web AssetType ID.

    var webTypeId = await MClient.Querying.SingleIdAsync(query);
    Note

    The MClient object is always available and can be used by all Script types. For more information about MClient, please refer to the SDK reference notes (MClient).

  12. If no AssetType ID is returned, the script exits.

    if (!webTypeId.HasValue) return;
  13. Retrieve the AssetTypeToAsset relation from the IEntity object using GetRelation.

    var relation = entity.GetRelation<IChildToOneParentRelation>("AssetTypeToAsset");
    Note

    AssetTypeToAsset is the relation between an Asset (child) and an AssetType (parent).

  14. If the relation does not exist, the script exits.

    if (relation == null) return;
  15. Set the AssetType Web to the asset by affecting the AssetType's ID to the relation's parent attribute.

    relation.Parent = webTypeId;
  16. This is an internal method used to get the extension of the filename.

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

Setup

  1. Create the asset type Web having the identifier M.AssetType.Web.
  2. Create, publish and enable an Action script.

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

  4. Create a new trigger and set the trigger's objective 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 action under Pre-commit actions.
  5. Save and enable the trigger.

Can we improve this article ? Provide feedback