Get removed asset

Use the following code to create an action to get removed assets.

RequestResponse
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Stylelabs.M.Sdk;
using Stylelabs.M.Sdk.Contracts.Base;

var executionId = string.Empty;

LogScriptCalled(Context, MClient);

//Get the Asset ID
var assetId = Context.TargetId.Value;

//Add the executionId to Property Bag
Context.PropertyBag.Add($"{assetId}-executionId", executionId);

//Get the change set (newly added assets)
var changeSet = Context.ChangeTracker.GetChangeSet();
var relationChanges = changeSet.RelationChanges;
var productGalleryRelationChange = relationChanges.FirstOrDefault(x => x.Name == "ProductToGalleryAsset");
var removedProductIds = productGalleryRelationChange.RemovedValues;

if (removedProductIds.Count <= 0)
{
    LogInfo(Context, MClient, $"There are no assets removed from the Product Gallery");
    return;
}

var strRemovedProductIds = string.Join(",", removedProductIds.ToArray());

LogInfo(Context, MClient, $"Product Gallery changed products: { strRemovedProductIds }");

//Add the asset IDs to Property Bag
Context.PropertyBag.Add($"{assetId}-{executionId}-change", strRemovedProductIds);


#region Helpers

void LogScriptCalled(IActionScriptContext context, IMClient client)
{
    executionId = Guid.NewGuid().ToString("N").Substring(0, 6);
    LogInfo(context, client, $"{executionId}: Execution started. Context: {context}");
}

void LogDebug(IActionScriptContext context, IMClient client, string message)
{
    client.Logger.Debug(LogPrefix(context) + message);
}

void LogInfo(IActionScriptContext context, IMClient client, string message)
{
    client.Logger.Info(LogPrefix(context) + message);
}

string LogPrefix(IActionScriptContext context)
{
    return $"{ (context?.TargetId != null ? context.TargetId.Value.ToString() : string.Empty)} | ";
}

#endregion

Do you have some feedback for us?

If you have suggestions for improving this article,