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:

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

    Add a new class in the M.AssetType in Taxonomy
  • Enter Web as the label for the new item:

    Add web to the new class in the M.AssetType in Taxonomy
  • Click Save.

Script

RequestResponse

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

To know which libraries to include, see Context properties (property types). The context property TargetType indicates the type of Target. The possible values of TargetType are Entity, EntityDefinition, Policy and DataSource.

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

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

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

    RequestResponse
    
    // Make sure that 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.

    RequestResponse
    
    var entity = Context.Target as IEntity;
    
    
    Warning

    The Target object type depends on the Trigger's objectives (for example, 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.

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

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

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

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

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

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

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

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

    RequestResponse
    
    if (!webTypeId.HasValue) return;
    
    
  13. Retrieve the AssetTypeToAsset relation from the IEntity object using GetRelation. AssetTypeToAsset is the relation between an Asset (child) and an AssetType (parent).

    RequestResponse
    
    var relation = entity.GetRelation<IChildToOneParentRelation>("AssetTypeToAsset");
    
    
  14. If the relation does not exist, the script exits.

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

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

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

Do you have some feedback for us?

If you have suggestions for improving this article,