Content Hub Tip #27: Refresh renditions

Sitecore Content Hub Tips


The usage of renditions really gives us an advantage when creating public links. Once in a while, you'll need to refresh your existing assets. When you decide to add a new rendition or change an existing one. Either way, sometimes you just need to refresh.

Refreshing your renditions can be done in two ways, either via the UI in Content Hub or via code. With code, I mean a script within Content Hub, writing code against de REST API or using one of the SDKs. Which way you prefer doesn't really matter, the thought will remain the same.

If you don't have many assets, I would recommend doing a refresh via the UI, because it's quite easy to do. In this blog post, I will therefore focus on refreshing renditions on a large scale. 

Warning
Use the script with caution. Always make sure to test it out first on a lower environment. Don't start with it on production! Also, use small batches of around 100 assets when refreshing. Only start a new batch, after the current batch has been fully completed. Otherwise, your customer will be frustrated that their assets don't get processed, because of your action.

var renditionNames = new List<string>
{
    // When empty, all renditions will be refreshed
    // or specify by rendition name; like Preview, Original, etc.
};

var targetIds = new List<long>
{
    32637
};

var jobEntity = await client.EntityFactory.CreateAsync("M.Job").ConfigureAwait(false);
jobEntity.SetPropertyValue("Job.Type", "MassEdit");
jobEntity.SetPropertyValue("Job.State", "Created");
jobEntity.SetPropertyValue("Job.Condition", "Pending");
jobEntity.SetPropertyValue("Job.TargetCount", Convert.ToInt64(0));
jobEntity.SetPropertyValue("Job.TargetsCompleted", Convert.ToInt64(0));
var jobId = await client.Entities.SaveAsync(jobEntity).ConfigureAwait(false);

// logger.LogInformation($"Created job {jobId}");

// then the description
var descriptionEntity = await client.EntityFactory.CreateAsync("M.JobDescription").ConfigureAwait(false);
string jobConfiguration = $@"{{
    ""$type"": ""Stylelabs.M.Base.MassEdit.MassEditJobDescription, Stylelabs.M.Base"",
    ""Operations"": [
        {{
            ""$type"": ""Stylelabs.M.Base.MassEdit.RefreshRenditionsOperation, Stylelabs.M.Base"",
            ""Renditions"": [{string.Join(",", renditionNames.Select(name => $"\"{name}\""))}],
			""FailedOnly"": false, // set to true, to only do failed renditions
""RefreshHistory"": false, ""RefreshSubfiles"": false }} ], ""FinalizeOperations"": [], ""Targets"": [{string.Join(",", targetIds)}] }}"; descriptionEntity.SetPropertyValue("Job.Configuration", JToken.Parse(jobConfiguration)); var jobRel = descriptionEntity.GetRelation("JobToJobDescription", RelationRole.Child); jobRel.SetIds(new long[] { jobId }); var descriptionId = await client.Entities.SaveAsync(descriptionEntity).ConfigureAwait(false); // logger.LogInformation($"Created job description {descriptionId}"); // And finally update the job (we can only do that after the description is created) var loadConfig = new EntityLoadConfiguration() { PropertyLoadOption = new PropertyLoadOption("Job.State", "Job.TargetCount"), RelationLoadOption = RelationLoadOption.None, CultureLoadOption = CultureLoadOption.Default }; jobEntity = await client.Entities.GetAsync(jobId, loadConfig).ConfigureAwait(false); jobEntity.SetPropertyValue("Job.State", "Pending"); jobEntity.SetPropertyValue("Job.TargetCount", Convert.ToInt64(targetIds.Count)); await client.Entities.SaveAsync(jobEntity).ConfigureAwait(false);

Happy coding', until next time!