Save sort order of assets
Use the following code to create an action to save the sort order of assets.
Note
For information on script parameters, see the API reference.
RequestResponse
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Stylelabs.M.Sdk;
using System.Globalization;
using System.Text;
LogScriptCalled(Context, MClient);
//Get the Product ID
var assetId = Context.TargetId;
if (assetId == null)
{
LogInfo(Context, MClient, "Asset ID is null");
return;
}
var assetLoadConfig = new EntityLoadConfiguration(
CultureLoadOption.Default,
PropertyLoadOption.None,
RelationLoadOption.None);
var assetEntity = await MClient.Entities.GetAsync(assetId.Value, assetLoadConfig).ConfigureAwait(false);
if (assetEntity == null)
{
LogInfo(Context, MClient, "Asset entity is null");
return;
}
string strExecutionId, strRemovedProductIds;
//Get the executionId from Property Bag
Context.PropertyBag.TryGetValue($"{assetId.Value}-executionId", out strExecutionId);
if (string.IsNullOrWhiteSpace(strExecutionId))
{
LogInfo(Context, MClient, "There is no executionId found");
return;
}
//Get the removed Product Ids from Property Bag
Context.PropertyBag.TryGetValue($"{assetId}-{strExecutionId}-change", out strRemovedProductIds);
if (string.IsNullOrWhiteSpace(strRemovedProductIds))
{
LogInfo(Context, MClient, "There are no Product Ids associated with this asset");
return;
}
LogInfo(Context, MClient, $"Product Gallery changed products: { strRemovedProductIds }");
var removedProductIdsList = strRemovedProductIds.Split(',').Select(p => long.Parse(p));
var productLoadConfig = new EntityLoadConfiguration(
CultureLoadOption.Default,
new PropertyLoadOption("ProductGalleryAssetsInSortedOrder"),
RelationLoadOption.None);
var productEntities = await MClient.Entities.GetManyAsync(removedProductIdsList, productLoadConfig);
foreach(var productEntity in productEntities)
{
if (productEntity == null)
{
LogInfo(Context, MClient, "Product entity is not found");
continue;
}
var productGalleryAssetsInSortedOrderProperty = productEntity.GetProperty<ICultureInsensitiveProperty>("ProductGalleryAssetsInSortedOrder");
if (productGalleryAssetsInSortedOrderProperty == null)
{
LogInfo(Context, MClient, $"ProductGalleryAssetsInSortedOrder property is not found for product with Id { productEntity.Id }");
continue;
}
var productGalleryAssetsInSortedOrderPropertyVal = productGalleryAssetsInSortedOrderProperty.GetValue<string>();
LogInfo(Context, MClient, $"ProductGalleryAssetsInSortedOrder property value is {productGalleryAssetsInSortedOrderPropertyVal}");
if (productGalleryAssetsInSortedOrderPropertyVal == null)
continue;
var newPropertyVal = string.Join(",", Array.FindAll(productGalleryAssetsInSortedOrderPropertyVal.Split(','), i => i != assetEntity.Identifier).ToArray());
LogInfo(Context, MClient, $"ProductGalleryAssetsInSortedOrder property new value is {newPropertyVal}");
productGalleryAssetsInSortedOrderProperty.SetValue(newPropertyVal);
await MClient.Entities.SaveAsync(productEntity).ConfigureAwait(false);
}
#region Helpers
void LogScriptCalled(IActionScriptContext context, IMClient client)
{
var 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);
}
void LogError(IActionScriptContext context, IMClient client, string message)
{
client.Logger.Error(LogPrefix(context) + message);
}
string LogPrefix(IActionScriptContext context)
{
return $"{ (context?.TargetId != null ? context.TargetId.Value.ToString() : string.Empty)} | ";
}
#endregion