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:
Identifier Label 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
AssetOriginis empty → set it toAssetOrigin.Demo - If
AssetOriginalready has a value → leave it as-is
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
- User uploads an asset through the Content Hub portal
AssetOriginis not set previous value is empty- Trigger fires and the Pre-commit script runs
- Script detects
AssetOriginis empty and sets it toAssetOrigin.Demo - 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
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
LoadMembersAsyncwithRelationLoadOption.None - Create a trigger with Objective
Entity creationand Execution typeIn 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.Demoautomatically - API uploads can pass
AssetOrigin.ApiSourceexplicitly — 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! 👋