Setting Default Field Values on Asset Upload in Sitecore Content Hub

Hello Sitecorians! 👋


Welcome to another exciting blog!


Today, we are going to talk about something I have personally come across in my project experience: setting default field values automatically when an asset is uploaded in Sitecore Content Hub.


To explain this, I am creating a simple use case, you can follow the same approach and adapt it to your own business requirements.


So, let's get started! 😊


The Use Case

For this blog, I am creating a property called AssetOrigin on the M.Asset entity. The idea is simple:

  • When an asset is uploaded through the UI, it should automatically get a default origin value.
  • When an asset is uploaded through the API, the caller can pass their own origin value and the script should respect it without overwriting it.


This is just a demo use case to explain the approach. In your real project, the property and default values will be different but the pattern remains the same


Step 1: Create the Option List

First, let us create the AssetOrigin option list.


Go to Manage → Option Lists.



Create a new option list called AssetOrigin and add the following two items:




IdentifierLabel
AssetOrigin.DemoDemo
AssetOrigin.ApiSourceApiSource


Here is what each value represents in our use case:

  • AssetOrigin.Demo - the default value that gets applied automatically when no origin is provided, for example during a UI upload.
  • AssetOrigin.ApiSource - the value that an API caller can explicitly pass in their payload to identify the asset as coming from an API source.


Again, these are demo values. In your project, you will define option list items that match your actual business needs.


Step 2 - Add AssetOrigin to the M.Asset Schema

Next, we add the AssetOrigin property to the M.Asset entity definition.


Go to Manage → Schema → M.Asset and add a new member:

  • Name: AssetOrigin
  • Type: OptionList
  • Option List: AssetOrigin


I have placed it under an Origin Information member group to keep things organised. Click Apply changes to save.


Step 3 - Write the Script

Now let us write the action script.


Go to Manage → Scripts and create a new Action Script.


The logic is straightforward:

  • If AssetOrigin is empty → set it to AssetOrigin.Demo
  • If AssetOrigin already has a value → leave it as-is

using System;
using System.Threading.Tasks;
using Stylelabs.M.Framework.Essentials.LoadConfigurations;
using Stylelabs.M.Scripting.Types.V1_0.Action;

// Property name
const string AssetOriginProperty = "AssetOrigin";

// Default value applied when no origin is provided (UI uploads)
const string DefaultAssetOrigin = "AssetOrigin.Demo";

// Step 1: Get the target asset entity
var asset = Context.Target as IEntity;
if (asset == null)
{
    MClient.Logger.Info("Target entity is null. Exiting.");
    return;
}

// Step 2: Load the AssetOrigin property
await asset.LoadMembersAsync(
    new PropertyLoadOption(AssetOriginProperty),
    RelationLoadOption.None
);

// Step 3: Read the current value
var currentValue = asset.GetPropertyValue<string>(AssetOriginProperty);

// Step 4: Set-if-empty
// If the API caller already passed a value (e.g. "AssetOrigin.ApiSource"),
if (string.IsNullOrEmpty(currentValue))
{
    MClient.Logger.Debug(
        $"AssetOrigin is empty. Setting default: {DefaultAssetOrigin}");

    asset.SetPropertyValue(AssetOriginProperty, DefaultAssetOrigin);

    MClient.Logger.Info(
        $"AssetOrigin set to default: {DefaultAssetOrigin}");
}
else
{
    MClient.Logger.Info(
        $"AssetOrigin already has value: '{currentValue}'. Skipping default.");
}


Step 4: Configure the Trigger

Now we wire everything together using a trigger.


Go to Manage → Scripts → Triggers and create a new trigger called SetAssetOrigin.


General Tab

Fill in the following details:



Conditions Tab

Go to the Conditions tab and set up the following condition:


This condition means the trigger will only fire when the previous value of AssetOrigin was empty


Actions Tab

Go to the Actions tab. Under Pre-commit actions, click Manage actions


Fill in the following:

  • Name: SetAssetOrigin
  • Action type: Action script
  • Script: SetAssetOrigin



Click Save and close on the trigger.


How It Works End to End

Let us quickly walk through all three scenarios:


Scenario 1 - Asset uploaded via UI

  1. User uploads an asset through the Content Hub portal
  2. AssetOrigin is not set previous value is empty
  3. Trigger fires and the Pre-commit script runs
  4. Script detects AssetOrigin is empty and sets it to AssetOrigin.Demo
  5. Asset is saved with the default value as part of the same transaction


Scenario 2 - Asset uploaded via API with explicit origin

API caller passes the following in the request payload:




Postman showing POST request with AssetOrigin.ApiSource, 201 Created with entity id 75337.


We then verify by calling GET on the created entity:




GET response confirming AssetOrigin is AssetOrigin.ApiSource, script respected the API-provided value


Our script detected that AssetOrigin was already populated and skipped the default entirely. The API-provided value is preserved as-is.


Conclusion

And that's a wrap! 🎉


As I mentioned at the start, this was a demo use case created purely to explain the approach. The property name, option list identifiers, and default values I used here are for illustration purposes only. In your real project, you will have your own properties and your own business-specific default values — but the approach will remain exactly the same.


Here is a quick summary of what we covered:

  • Create an Option List with your default and source-specific identifiers
  • Add the property to the M.Asset schema as an OptionList type
  • Write a script in the Content Hub script editor using the set-if-empty pattern wrapped in a try-catch
  • Use LoadMembersAsync with RelationLoadOption.None
  • Create a trigger with Objective Entity creation and Execution type In process
  • Set the condition to AssetOrigin → previous value → is empty
  • Add the script as a Pre-commit action so changes are part of the same save transaction
  • UI uploads get AssetOrigin.Demo automatically
  • API uploads can pass AssetOrigin.ApiSource explicitly — and the script will always respect it


I hope this helps you in your Content Hub journey! If you have any questions, feel free to drop a comment below. 😊


Stay tuned for more! 👋


Post a Comment (0)
Previous Post Next Post