Viewing Asset XMP Data in Sitecore Content Hub
In this post, we will cover extracting and viewing digital asset information embedded into files using the Extensible Metadata Platform (XMP) standard. We’ll take a look at the steps needed to extract the information and view it as part of the digital assets’ detail view. The implementation requires elevated access rights to be able to create a custom script and extend asset schema, and optionally update asset display view. There is no shortcut or TL;DR here; however, I’ll keep it focused and concise.
What is XMP?
XMP is an industry standard for storing digital asset descriptive and contextual information developed by Adobe. To get more detail about the format, please reference the Adobe’s XMP information page. The XMP metadata is especially important in rights management, asset tagging, and taxonomies.
Updating Asset Schema
The first step is to update asset schema to add fields for displaying the XMP information. The number of fields and their types will depend on the amount of XMP information that needs to be extracted. In this example, we’ll focus on adding one field, as the same approach can be used for adding more if needed.
To update asset schema, navigate to Manage > Schema and click on M.Asset. In the M.Asset view, select the section you’d like to add the new field to, or create a new one, then click “NEW MEMBER” button to start adding a new property to the M.Asset entity.
Since the information that we are importing here is simple text and not a complex object, we will pick the Property option in the new member type prompt, then String for the format and click NEXT. The following window allows us to configure additional information about the field. For this example, we will leave the defaults in place and only update the Name and Label fields. The label is optional, but it’s one of my rules to have user-friendly labels, especially since its best to keep names developer-friendly for API access. We’ll set the name to “CameraBrand” and label – “Camera Brand.”
Click SAVE to add the property and Publish for the change to become effective.
NOTE: Once added to the section that’s already displayed on the asset detail page, the property will automatically be visible on asset detail pages. In cases when adding to a new property group or a section that’s not displayed, navigate to Manage > Pages and search for Asset Detail Page. After clicking on the page, you’ll be presented with a component-based representation of the asset detail page template. Here you may choose to either create a new section to display the metadata or add it to an existing one.
Create a Script to Extract XMP Information in Content Hub
Navigate to Manage > Scripts and click NEW SCRIPT. In the prompt specify the name, make sure to set the type to “Metadata processing”, and click SAVE. To start updating the newly created script, click the “…” on the right of the script line item and click Edit. Once in the editing view, copy and paste the following code –
using System.Linq; var masterFileRelation = await Context.File.GetRelationAsync("MasterFile"); if (!masterFileRelation.Parents.Any() || !masterFileRelation.Parents.Contains(Context.Asset.Id.Value)) { return; } string ToCsvValue(object source) { var str = source.ToString(); if (str.Contains(",")) { return "\"" + str + "\""; } return str; } var cameraBrand = await Context.Asset.GetPropertyAsync("CameraBrand"); if(Context.MetadataProperties.ContainsKey("make")){ cameraBrand.SetValue(Context.MetadataProperties["make"]?.ToString()); } await MClient.Entities.SaveAsync(Context.Asset); MClient.Logger.Info($"XMP metadata processed for asset ID {Context.Asset.Id}.");
Click BUILD and wait for the system to build the script; once successfully completed, click PUBLISH. Once published, click the back arrow in the top-left corner to go back to the script listing view, or navigate to Manage > Scripts and make sure that the script is enabled by ensuring it has a green check ticked on the line-item. That’s it!
To test the script, try uploading the following small image that has XMP data embedded in it, then checking the detail page (note that by default, empty fields are hidden on the asset detail view).