Go to the homepage
Powered bySitecore Search logo
Skip to main contentThe Conditionally generate renditions page has loaded.

Conditionally generate renditions

How to conditionally generate a predefined set of renditions by attaching an additional Media Processing Matrix

Last updated: Dec 13, 2024

Context

Processing additional renditions results in additional consumption of Media storage and increases the time to process your Asset. Sometimes these additional renditions are only needed when certain conditions are met.

As we want to save some storage space we will attach an additional Media Processing Matrix through the Trigger and Actions framework. This allows us to pre configure additional renditions and to conditionally generate these renditions for specific Assets.

The provided code is intended as a guideline and must be tailored to suit your specific implementation requirements. Please ensure thorough end-to-end testing is conducted to validate its functionality and performance in your environment.

Execution

The following walks through the steps of pre-configuring additional redentions and generating them for specific Assets.

Add a new Media Processing Matrix

This will allow us to pre-configure renditions that can be used for Social media.

  1. Go to Manage > Media Processing as a Super User.
  2. Click the “New set” button and create a new set with the following details:
    • Name: Social
    • Auto-run: disabled
    New Set on Media Processing
  3. Once your new set has been created, you can go ahead and configure your desired media processing flow.
  4. Next Save and Publish your newly created Media Matrix to make it available.Media Matrix

Create a Script & Action to attach the Matrix

Create a Script & Action that attaches your newly-created media matrix to a Target Asset. More detailed guidlines on Scripts can be found on the Scripts Guidance and Scenarios recipe.

  • Go to Manage > Script as a Super User.
  • Create a new Script with the following details:
    • Name: PREFIX - Add Social Media Processing Matrix
    • Type: Action
    New Script to attach to matrix
  • Once created,create a script that retrieves the asset from the context against the new Media Matrix. Then Build, Publish and Enable the script.
using System.Linq; //Retrieve the asset from the Context var asset = Context.Target as IEntity; //Define the name of the MediaMatrix entity string mediaMatrixSetName = "Social"; //Create the query to search for the MediaMatrix var query = Query.CreateQuery( entities => from e in entities where e.DefinitionName == "M.MediaMatrix" && e.Property("MediaMatrixName") == mediaMatrixSetName select e); //Search for the MediaMatrix entity IIdQueryResult queryResult = await MClient.Querying.QueryIdsAsync(query); //If no result, throw exception if(!(queryResult?.Items.Count > 0)) { var message = $"The MediaMatrix {mediaMatrixSetName} is not found."; MClient.Logger.Error(message); throw new Exception(message); } //Retrieve the entity from the result var mediaMatrixID = queryResult.Items.First(); //Search for the relation between the MediaMatrix and the Asset var mediaMatrixRelation = await asset.GetRelationAsync<IChildToManyParentsRelation>("MediaMatrixToAsset"); //Check if the asset is already linked to the specified Media Matrix if(!mediaMatrixRelation.Parents.Contains(mediaMatrixID)) { mediaMatrixRelation.Parents.Add(mediaMatrixID); await MClient.Entities.SaveAsync(asset); MClient.Logger.Info($"{mediaMatrixSetName} set relation added for asset {asset.Id.Value}."); } else { MClient.Logger.Info($"{mediaMatrixSetName} has been already added for asset {asset.Id.Value}."); }
  • Next navigate to Actions (Manage > Actions).
  • Create a New Action by Hitting the “New Action” button with the following details:
    • Name: PREFIX - Add Social Media Processing Matrix
    • Type: Action Script
    • Script: PREFIX - Add Social Media Processing Matrix (this is your previously created Script)
    New Action to attach to matrix

Create a Trigger for specific Conditions

Create a trigger that will be executed when specific conditions are met.

  • Navigate to Manage > Triggers as a Super User
  • Create a new trigger by clicking the New Trigger button.
    • General - Fill in the General tab with the following details:
      • Name: PREFIX - Add Social Media Processing Matrix
      • Objective: Entity Creation, Entity Modification. The Objective in combinations with the Conditions allows us to define when the trigger will be executed.
      • Execution type: In Background. Use ‘In Background’ where possible as ‘In Process’ blocks the entity save process.
    • New Trigger based on condition
    • Conditions - Fill in the Conditions tab with the following details:
      • Add the “M.Asset” target definition as we want to run the trigger for Assets.
      • We will first define to which conditions the asset need to apply in order to run the Action (the current state of the Asset):
        • Add Content Repository and Final Lifecycle conditions first as a good practice:
          • Final Lifecycle: Created, Under Review, Approved
          • Content Repository: Standard
        • Add an additional condition to filter on Asset Type Social.
        • Finally we will also check if the Social media processing matrix isn’t attached to our Asset Yet by adding a filter for Media Matrix.
      • The second part of the Condition is a good place to reserve for identifying what has changed on the entity (we are only interested in relevant changes). Add an additional filter on the Asset Type relation changes. Additional filter on the Asset Type relation changes As a good practice, try to put as many conditions as possible that fit the situation on the trigger so we avoid any unnecessary executions of our script. In the above example we split up 1) Conditions that indicate the current state of the entity (top), and 2) Conditions that checks any relevant changes made on the entity (bottom).
    • Actions - As we are using an "In Background” execution style trigger we can only configure “Post Actions”. To finish the trigger setup, point the Post action towards the PREFIX - Add Social Media Processing Matrix script that we have created.Point the Post action towards the script created

When adding the Social Taxonomy to your asset, the Facebook and Instagram renditions are now automatically generated.

Debugging

  • Actions Auditing - we can check and verify that the trigger has run from the Actions Auditing screen. Go to Manage > Actions as a Super User and on the Audting tab we can see any trigger execution. Trigger Execution logs
  • Script Logging - as there might be something wrong with your script, you can also check script logging. Navigate to your script and hit the “view logs” button.Script Execution logs

© Copyright 2024, Sitecore. All Rights Reserved