アセットのソート順の保存
日本語翻訳に関する免責事項
このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。
次のコードを使用して、アセットの並べ替え順序を保存するアクションを作成します。
メモ
スクリプトパラメータの詳細については、APIリファレンスを参照してください。
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