Sitecore Content Hub - Custom Trigger/Action to clean an Asset's title

Today I want to show you how easy it is to modify the properties of an asset using a custom Trigger/Action. In this specific scenario, we are going to clean the asset's title to follow certain naming conventions when the asset is created. The naming conventions that we are going to enforce are: 

1. Replace empty spaces with hyphens 

2. Replace underscores with hyphens

 3. Remove special characters 

4. Use only lowercase letters

The first thing we need to do is create our custom Script:  



The code we are going to use in this script is the following: 

 using Stylelabs.M.Sdk;  
 using System.Net;  
 using System.Net.Http;  
 using System.Text;  
 using Newtonsoft.Json;  
 using System.IO;  
 using System.Text.RegularExpressions;  
 var assetLoadConfiguration = new EntityLoadConfiguration()  
 {  
   PropertyLoadOption = new PropertyLoadOption("Title"),  
   RelationLoadOption = RelationLoadOption.None,  
   CultureLoadOption = CultureLoadOption.Default  
 };  
 var assetId = Context.TargetId.Value;  
 var asset = await MClient.Entities.GetAsync(assetId, assetLoadConfiguration);  
 if (asset != null)  
 {  
   var currentTitle = asset.GetPropertyValue<string>("Title");  
   var cleanTitle = Regex.Replace(currentTitle, @"[^0-9a-zA-Z.]+", "-");  
   asset.SetPropertyValue("Title", cleanTitle.ToLower());  
   MClient.Logger.Info($"rename {currentTitle} to {cleanTitle}");  
 }  
 await MClient.Entities.SaveAsync(asset).ConfigureAwait(false);  


Now let's create our Action:



Lastly, let's create our Trigger:





Now after an asset gets created the title property will get updated using the rules we defined above! 

Happy Scripting! 

Comments