Sitecore Content Hub - Custom Action/Trigger to assign Asset Types automatically

Assigning Asset Types is usually a manual task that is performed when an asset is created. However, we can define some rules so that the Asset Types are automatically assigned based on a condition. Today I want to show you how easy it is to accomplish this by using a custom Trigger/Action. This is something we can define for types of assets that are continuously uploaded to Content Hub. In my specific scenario, I want to automatically assign the Asset Type depending on keywords existing on the file name as follow:

1. If the file name contains the "sow" keyword, assign the SOWs Asset Type

2. If the file name contains the "contract" keyword, assign the Contracts Asset Type

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.Base.Querying;  
 using Stylelabs.M.Base.Querying.Linq;  
 using Stylelabs.M.Framework.Essentials.LoadOptions;  
 using Stylelabs.M.Sdk;  
 using Stylelabs.M.Sdk.Contracts.Base;  
 using System;  
 using System.IO;  
 using System.Linq;  
 using System.Threading.Tasks;  
 var asset = Context.Target as IEntity;  
 // Ensure the following members are loaded  
 await asset.LoadMembersAsync(new PropertyLoadOption(Constants.Asset.FileName), new RelationLoadOption(Constants.Asset.AssetTypeToAsset));  
 if (asset != null)  
 {  
   var fileName = asset.GetPropertyValue<string>(Constants.Asset.FileName).ToLower();  
   IEntity assetType = null;  
   if(fileName.Contains(Constants.Keywords.Sow)){  
     assetType = await MClient.Entities.GetAsync($"{Constants.AssetType.AssetTypeDefinition}{Constants.AssetType.Sows}").ConfigureAwait(false);  
   }else if (fileName.Contains(Constants.Keywords.Contract)){  
     assetType = await MClient.Entities.GetAsync($"{Constants.AssetType.AssetTypeDefinition}{Constants.AssetType.Contracts}").ConfigureAwait(false);  
   }  
   if(assetType == null) return;  
   var assetTypeToAssetRelation = asset.GetRelation<IChildToOneParentRelation>(Constants.Asset.AssetTypeToAsset);  
   assetTypeToAssetRelation.Parent = assetType.Id.Value;  
   await MClient.Entities.SaveAsync(asset).ConfigureAwait(false);  
 }  
 public static class Constants{  
   public static class Keywords{  
     public const string Sow = "sow";  
     public const string Contract = "contract";  
   }  
   public static class AssetType{  
     public const string AssetTypeDefinition = "M.AssetType.";  
     public const string Sows = "SOWs";  
     public const string Contracts = "Contracts";  
   }  
   public static class Asset{  
     public const string AssetTypeToAsset = "AssetTypeToAsset";  
     public const string FileName = "FileName";  
   }  
 }  

Now let's create our Action:




Lastly, let's create our Trigger:







Now after an asset gets created the Asset Type will automatically get populated if the file name contains one of the keywords defined above

Happy Scripting! 

Comments