Action script with a validation trigger
This example shows an action web script that verifies that an asset satisfies specific criteria, such as having required fields or valid relationships, before allowing it to be saved or published. The script is connected to a validation trigger that verifies data integrity before committing changes.
-
Ensure the asset or assets referenced in the script exist in the Content Hub schema.
-
Define any required properties, such as
TitleorDescription, 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;
var filename = await entity.GetPropertyValueAsync<string>("FileName");
if (string.IsNullOrEmpty(filename)) return;
var extension = GetExtension(filename)?.ToLowerInvariant();
if (string.IsNullOrEmpty(extension)) return;
if (!webExtensions.Contains(extension))
{
throw new ValidationException(
"The asset is not valid.",
new ValidationFailure("The file's extension must be the extension of a valid web filetype.", filename));
}
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.
-
Include the libraries to use in the script. To understand which libraries to include, see Context properties (property types).
RequestResponseusing System.Linq; -
Define valid web extensions. If an asset's file extension is not in this list, then its filename is not considered valid.
RequestResponsevar webExtensions = new [] { ".jpg", ".jpeg", ".png", ".gif" }; -
Retrieve the
Targetobject from theContextand casts it toIEntity.Targetis the asset being created or modified.RequestResponsevar entity = Context.Target as IEntity;WarningThe
TargetTypecontext property defines the type ofTarget, which can beEntity,EntityDefinition,Policy, orDataSource. TheTargetobject type depends on the trigger's objectives, such as entity, entity definition, or policy. Since this trigger is for entity creation and entity modification,Targetmust be cast toIEntity. -
Retrieve the
Filenameproperty from theTargetobject usingGetPropertyValueAsync.RequestResponsevar filename = await entity.GetPropertyValueAsync<string>("FileName");NoteThe scripting API uses lazy loading.
GetPropertyValueAsyncchecks whether the property is already loaded. If it's not, the script will load it for the user. -
If the asset doesn't have a filename, the script exits.
RequestResponseif (string.IsNullOrEmpty(filename)) return; -
The
GetExtensionfunction, which is defined at the end of the script, extracts the file extension from the file path. The script then usesGetExtensionto obtain the extension from the filename. It splits the path by periods (.) and returns the last segment prefixed with a dot (.) if there are multiple segments. If no extension is found, it returnsnull.RequestResponsestring GetExtension(string path) { var tokens = path.Split('.'); if (tokens.Length > 1) { return "." + tokens[tokens.Length - 1]; } return null; } -
Retrieve the extension from the filename using
Path.GetExtension (System.IO).RequestResponsevar extension = GetExtension(filename)?.ToLowerInvariant(); -
If the filename does not have an extension, the script exits.
RequestResponseif (string.IsNullOrEmpty(extension)) return; -
If the extension is not a valid web extension, the script throws a
ValidationException.RequestResponseif (!webExtensions.Contains(extension)) { throw new ValidationException( "The asset is not valid.", new ValidationFailure("The file's extension must be the extension of a valid web filetype.", filename)); }NoteFor more information about
ValidationExceptionandValidationFailure, as well as a list of the available custom exceptions, see the SDK reference.
Setup
-
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 new action under Validation actions.
-
-
Save and activate the trigger.