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
- A user creates or modifies an asset.
- 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;
// 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
-
Include the libraries to be used in the script.
RequestResponseusing System.Linq;
TipTo know which libraries to include, please refer to the Context properties section (property types).
-
Define the list of extensions. If an asset's filename has one of these extensions, it will be marked with the AssetType Web.
RequestResponsevar webExtensions = new [] { ".jpg", ".jpeg", ".png", ".gif" };
-
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"));
-
Retrieve the
Target
object from theContext
and cast it toIEntity
.Target
is the asset involved in the creation or modification event.RequestResponsevar entity = Context.Target as IEntity;
WarningThe
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 castTarget
toIEntity
.TipThe context property
TargetType
indicates the type ofTarget
. The possible values ofTargetType
areEntity
,EntityDefinition
,Policy
andDataSource
. For more information about context properties, please refer to the Context properties section. -
Retrieve the
Filename
property from theTarget
object usingGetPropertyValue
.RequestResponsevar filename = entity.GetPropertyValue<string>("FileName");
-
If the asset does not have a filename, the script exits.
RequestResponseif (string.IsNullOrEmpty(filename)) return;
-
Get the extension from the filename using
Path.GetExtension
(System.IO).RequestResponsevar extension = Path.GetExtension(filename)?.ToLowerInvariant();
-
If the filename does not have an extension, the script exits.
RequestResponseif (string.IsNullOrEmpty(extension)) return;
-
If the filename has an extension, but it is not one of the specified extensions, the script exits.
RequestResponseif (!webExtensions.Contains(extension)) return;
-
Create a query that retrieves the ID of the AssetType Web with identifier M.AssetType.Web.
RequestResponsevar query = Query.CreateQuery(entities => from e in entities where e.Identifier == "M.AssetType.Web" select e);
-
Use the query to retrieve The web AssetType ID.
RequestResponsevar webTypeId = await MClient.Querying.SingleIdAsync(query);
NoteThe
MClient
object is always available and can be used by all Script types. For more information aboutMClient
, please refer to the SDK reference notes (MClient). -
If no AssetType ID is returned, the script exits.
RequestResponseif (!webTypeId.HasValue) return;
-
Retrieve the
AssetTypeToAsset
relation from theIEntity
object usingGetRelation
.RequestResponsevar relation = entity.GetRelation<IChildToOneParentRelation>("AssetTypeToAsset");
NoteAssetTypeToAsset
is the relation between an Asset (child) and an AssetType (parent). -
If the relation does not exist, the script exits.
RequestResponseif (relation == null) return;
-
Set the AssetType Web to the asset by affecting the AssetType's ID to the relation's parent attribute.
RequestResponserelation.Parent = webTypeId;
-
This is an internal method used to get the extension of the filename.
RequestResponsestring GetExtension(string path) { var tokens = path.Split('.'); if (tokens.Length > 1) { return "." + tokens[tokens.Length - 1]; } return null; }
Setup
- Create the asset type Web having the identifier M.AssetType.Web.
-
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 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.
-
Save and enable the trigger.