Generate public links containing the asset filename
This action script example describes how to create a public link that includes the filename of its associated asset. The action occurs in the pre-commit phase (in-process), meaning the script is executed before the triggering changes are made – in this example, it happens whenever a public link is created.
-
Confirm that the schema includes the necessary properties for generating public links, such as asset identifiers.
-
Ensure that users or roles who run this script have the correct permissions that allow public link generation.
Script
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 following libraries:
-
Retrieve the
Targetobject from theContextand casts it toIEntity. TheTargetrepresents the public link being processed. If theTargetIdisnull, the script ends without performing any further actions. -
Ensure that specific properties (
RelativeUrl,Resource) and relations (AssetToPublicLink) are loaded for the public link entity. This is necessary for the script to manipulate these properties and relations later. -
Retrieve the ID of the asset associated with the public link via the
AssetToPublicLinkrelation. If the public link is not associated with an asset, the script logs a warning and then exits. -
If the public link is associated with an asset, retrieve the asset's ID and loads the corresponding asset entity. The script specifically loads the
FileNameproperty of the asset, which it will use to generate the URL. -
Generate a unique prefix using a GUID (globally unique identifier). This prefix helps ensure that the relative URL is unique, especially when multiple public links might point to the same resource or rendition.
-
Create the relative URL by combining the generated prefix, the resource name from the public link, and the file name of the associated asset. This relative URL is then set as the value of the
RelativeUrlproperty of the public link entity. -
Save the updated public link entity, ensuring that the changes are persisted.
Setup
To set up your script, action, and trigger:
-
Create a script of the Action type; save your changes, build and publish the script, and then enable it.
-
Create an action of the Action script type and link it to your script.
-
-
On the General tab, set the Objective to Entity creation.
-
On the General tab, set the Execution type to In process.
-
On the Conditions tab, add a condition for the M.PublicLink entity definition.
-
Under this definition, add a new condition for the Assets(AssetToPublicLink) entity definition and set it to has changed.
-
On the Actions tab, add the action created in step 2 under Pre-commit actions.
-
Save and activate the trigger.
-