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
using System.Linq;
var publicLink = Context.Target as IEntity;
// Ensure that the following members are loaded
await publicLink.LoadMembersAsync(new PropertyLoadOption("RelativeUrl", "Resource"), new RelationLoadOption("AssetToPublicLink"));
var assetToPublicLinkRelation = publicLink.GetRelation<IChildToManyParentsRelation>("AssetToPublicLink");
var assetToPublicLinkRelationValues = assetToPublicLinkRelation.GetIds();
// Ensure that the public link is linked to an asset, exit otherwise
if (assetToPublicLinkRelationValues?.Count > 0)
MClient.Logger.Info(String.Join(",", assetToPublicLinkRelationValues.Select(x => x.ToString())));
else
{
MClient.Logger.Warn($"Public link with id {publicLink.Id} is not associated with an asset");
return;
}
// Get the asset id
var assetId = assetToPublicLinkRelationValues.First();
// Populate the load configuration
var loadConfig = new EntityLoadConfiguration(
CultureLoadOption.Default,
new PropertyLoadOption("FileName"),
RelationLoadOption.None);
// Load the asset
var asset = await MClient.Entities.GetAsync(assetId, loadConfig);
if (asset == null)
{
MClient.Logger.Error($"Could not load asset with id: {assetId}");
return;
}
// Generate a prefix in case we have multiple public links with the same resource/rendition
var prefix = Guid.NewGuid().ToString("n").Substring(0, 8);
// Properties required for the relative url
var filename = asset.GetPropertyValue<string>("FileName");
var resource = publicLink.GetPropertyValue<string>("Resource");
// Compose the relative url
var relativeUrl = $"{prefix}-{resource}-{filename}";
// Update the relative url
publicLink.SetPropertyValue("RelativeUrl", relativeUrl);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:
RequestResponseusing System.Linq; using Stylelabs.M.Base.Querying; using Stylelabs.M.Base.Querying.Filters; using Stylelabs.M.Sdk.Contracts.Base; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using Stylelabs.M.Framework.Essentials.LoadConfigurations; -
Retrieve the
Targetobject from theContextand casts it toIEntity. TheTargetrepresents the public link being processed. If theTargetIdisnull, the script ends without performing any further actions.RequestResponseif (Context.TargetId == null) return; var publicLink = Context.Target as IEntity; -
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.RequestResponseawait publicLink.LoadMembersAsync(new PropertyLoadOption("RelativeUrl", "Resource"), new RelationLoadOption("AssetToPublicLink")); -
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.RequestResponsevar assetToPublicLinkRelation = publicLink.GetRelation<IChildToManyParentsRelation>("AssetToPublicLink"); var assetToPublicLinkRelationValues = assetToPublicLinkRelation.GetIds(); if (assetToPublicLinkRelationValues?.Count > 0) MClient.Logger.Info(String.Join(",", assetToPublicLinkRelationValues.Select(x => x.ToString()))); else { MClient.Logger.Warn($"Public link with id {publicLink.Id} is not associated with an asset"); return; } -
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.RequestResponsevar assetId = assetToPublicLinkRelationValues.First(); var loadConfig = new EntityLoadConfiguration( CultureLoadOption.Default, new PropertyLoadOption("FileName"), RelationLoadOption.None); var asset = await MClient.Entities.GetAsync(assetId, loadConfig); if (asset == null) { MClient.Logger.Error($"Could not load asset with id: {assetId}"); return; } -
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.
RequestResponsevar prefix = Guid.NewGuid().ToString("n").Substring(0, 8); -
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.RequestResponsevar filename = asset.GetPropertyValue<string>("FileName"); var resource = publicLink.GetPropertyValue<string>("Resource"); var relativeUrl = $"{prefix}-{resource}-{filename}"; publicLink.SetPropertyValue("RelativeUrl", relativeUrl); -
Save the updated public link entity, ensuring that the changes are persisted.
RequestResponseawait MClient.Entities.SaveAsync(publicLink).ConfigureAwait(false);
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.
-