We will extend the asset schema to automatically set metadata from EXIF data recorded by camera manufacturers. This section provides the procedure and related information for importing and using this metadata.
What is XMP data?
In order to support XMP (Extensible Metadata Platform), digital asset management tools must be able to provide metadata about digital assets. Sitecore Content Hub provides standard metadata and can be extended as needed.
The following data will be displayed as properties of the file after standard import.
This scenario
One of the data contained in the photo is the name of the camera manufacturer. The following is a procedure for extending this into the schema and setting it up automatically.
- Camera brand
Sample images to be used as a test were downloaded from the following site
Schema Extension
The first step is to extend the schema to create an entry for the camera brand.
- Open the Schema in the admin panel
- Select M.Asset as the schema type
- Select the group in the Overview, then click New Member.
- As for the item, this time select Properties and go next.
- Select String for property type and go to next
- Enter the schema entry (in this case CameraBrand )
- After saving, publish the schema
This results in an extended schema.
Creating Scripts
Sample scripts for processing metadata can be found at the following sites
First, paste the code as is. (Detailed explanation of the process can be found on the page, so we will only introduce the process here.)
using System.Linq;
var masterFileRelation = await Context.File.GetRelationAsync<IChildToManyParentsRelation>("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 headers = string.Join(", ", Context.MetadataProperties.Keys.Select(ToCsvValue));
var values = string.Join(", ", Context.MetadataProperties.Values.Select(ToCsvValue));
var metadataProp = await Context.Asset.GetPropertyAsync<ICultureInsensitiveProperty>("Metadata");
metadataProp.SetValue(headers + "\n" + values);
await MClient.Entities.SaveAsync(Context.Asset);
This script takes metadata information as a CSV and outputs it to the Metadata property of the asset. For this part, we will process it as JSON data and use JToken. Then, in the JSON data, the camera brand is listed as _make_, so add a definition.
using System.Linq;
using Newtonsoft.Json.Linq;
string cameraBrandKey = "make";
MClient.Logger.Info("Metadata Mapping starts.");
var masterFileRelation = await Context.File.GetRelationAsync<IChildToManyParentsRelation>("MasterFile");
if (!masterFileRelation.Parents.Any() || !masterFileRelation.Parents.Contains(Context.Asset.Id.Value))
{
return;
}
JToken cameraBrand;
if(Context.MetadataProperties.TryGetValue(cameraBrandKey, out cameraBrand))
{
Context.Asset.SetPropertyValue("CameraBrand", cameraBrand.ToString());
}
else
{
MClient.Logger.Info($"Could not find a value for {cameraBrandKey}");
Context.Asset.SetPropertyValue("CameraBrand", $"{cameraBrandKey} not found");
}
await MClient.Entities.SaveAsync(Context.Asset);
If you are adding other keys, you can increase the number of items by creating multiple parts of the JToken description.
Register Scripts
When registering a script, you must select the type of metadata processing, as this time it will be a script to be executed in media processing.
The next step is to register, build, and activate the script.
Once registration is complete, the script should be activated.
Testing and confirmation of results
When the script is enabled, the setup is complete. The registered script is executed at the time of media processing when a file is uploaded. Therefore, as a test, when a file is uploaded, data is automatically set from the _make_ item to the item extended as the schema.
Summary
As described above, data held as file properties can be expanded into a schema, and faceted settings, refinements, etc. can be made using the data expanded into the schema. The schema will be expanded and scripts will be registered according to the actual operation.