Enabling search by asset ID in ContentHub

In some cases, authors might want to search ContentHub for a DAM asset by its ID. Out of the box, this is not possible via the search GUI but it can be enabled with a trick:

Add field to M.Asset’s Schema

The field doesn’t need to be displayed on any pages as it will be populated automatically.

Create script and trigger

  1. Create, compile and publish a script under Manage > Scripts:
using Stylelabs.M.Sdk;
using Stylelabs.M.Sdk.Contracts.Content;

try {        
    long assetId = Context.TargetId.Value;
    var client = MClient;
    var assetToUpdate = await client.Entities.GetAsync(assetId);

    // Write Asset ID to SearchableAssetId field (for search in fulltext index)
    if (assetToUpdate.GetPropertyValue<string>("SearchableAssetId") != assetId.ToString()) {
        MClient.Logger.Info($"Setting SearchableAssetId to: {assetId}");
        assetToUpdate.SetPropertyValue("SearchableAssetId", assetId.ToString());
        client.Entities.SaveAsync(assetToUpdate);
    }
}
catch (Exception e) {
    MClient.Logger.Info(e.ToString()); 
    return;
}

2. Create an action that references your script

3. Create a trigger, that fires when “SearchableAssetId” is empty

General Tab
Conditions tab
Actions tab

Testing

In order to test, you’ll need to re-save a DAM asset. This will trigger our script asychronously and eventually update the search index. It might take a few minutes for the index to be updated.

When all is done, you should be able to search for assets by their IDs.


Leave a Reply

Your email address will not be published. Required fields are marked *