Skip to content
Home Sinan Eldem
Software

Update Asset Description for Sitecore Content Hub

Sinan Eldem

• 3 min read

Update Asset Description for Sitecore Content Hub

Sample C Sharp code I've written for updating Asset Description for Sitecore Content Hub.

You can use it as you like, you may even change and develop your own code.

using System.Globalization;

long assetId = -1;
IEntity thisAsset = null;
string assetTitle = null;
string assetFileName = null;
string assetDescription = null;
string updatedDescription = null;
CultureInfo culture = new CultureInfo("en-US", false);

try
{
    MClient.Logger.Info($"Begin script run...");
    long? tempId = Context.TargetId;

    if(tempId.HasValue)
    {
        assetId = tempId.Value;
        MClient.Logger.Info($"The Asset Id is {assetId}");

        thisAsset = await MClient.Entities.GetAsync(assetId);

        MClient.Logger.Info($"<p>The Asset Identifier is {thisAsset.Identifier}, and entity definition name is {thisAsset.DefinitionName}</p>");

        assetTitle = thisAsset.GetPropertyValue<string>("Title");
        assetFileName = thisAsset.GetPropertyValue<string>("FileName");
        updatedDescription = $"The asset title is {assetTitle}, and file name is {assetFileName}.";

        MClient.Logger.Info(updatedDescription);
        thisAsset.SetPropertyValue("Description", culture, updatedDescription);
        await MClient.Entities.SaveAsync(thisAsset);
    }
    else
    {
        MClient.Logger.Info($"The TargetId is null, exiting.");
        throw new Exception("Null value exception, No TargetId in the Context");
    }
}
catch (Exception e)
{
    MClient.Logger.Error($"{e.Message}");
}

Sinan Eldem

Custom software and AI solutions with Laravel

Related posts

Software
6 min read

Laravel Eloquent Relationships Quick Reference Guide

This quick reference is a practical guide that explains the most common relationship types with simple examples. Covering hasOne, hasMany, belongsTo, belongsToMany and polymorphic relationships along with eager loading and performance tips, it gives developers a fast and accurate reference.

Read more