Keeping track of ContentHub assets used in Sitecore XM

ContentHub integrates nicely with Sitecore XM through Sitecore Connect for ContentHub. Probably the most popular use case is referencing assets stored in ContentHub’s DAM instead of using classic Media Items. This is integrated nicely through an extension of the image field:

When an asset and public link is selected from ContentHub, the following information is stored in the image field’s raw value:

<image 
	stylelabs-content-id="my-asset-id" 
	thumbnailsrc="https://my-content-hub-instance/api/gateway/my-asset-id/thumbnail" 
	src="https://my-content-hub-instance-delivery/api/public/content/my-public-link" 
	mediaid="" 
	stylelabs-content-type="Image" 
	alt="my alt text" 
	height="667" 
	width="1000" />

Note that compared to classic Media Items, no item in the Media Library exists for content hub assets. All asset information is stored solely in the image fields as XML attributes. We therefore cannot keep track of asset usage through the link database.

Storing asset references in an index

A solution to this problem is to add all referenced ContentHub Asset IDs to an index. We can then query that index to find which items are referencing a specific asset.

For that, we need a computed field:

And we need to add this computed field through patch config (in this example we are patching the sitecore_master_index):

And we need a SearchResultItem to use in linq queries:

Finding asset references

Having the referenced assetIds stored in an index, we can now perform queries:

long assetId = 12345;  // ContentHub assetId to query for

ISearchIndex index = ContentSearchManager.GetIndex("sitecore_master_index");
using (IProviderSearchContext context = index.CreateSearchContext())
{
	var results =
		context.GetQueryable<ContentHubImagesSearchResultItem>()
		.Where(i => i.ContentHubAssetIds.Contains(assetId.ToString()))
		.GetResults();
}
Series NavigationDisplay CMS usage on ContentHub’s asset details page >>

Leave a Reply

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