---
title: "インベントリエンティティビューズ&アクションAPIの活用"
slug: "work-with-the-inventory-entity-views-and-actions-api"
abstract: "XCインベントリ業務およびデータを外部ビジネスシステムと統合するために使用されるXCインベントリエンティティビューおよびアクションAPIの概要。"
---
# インベントリエンティティビューズ&アクションAPIの活用

[XCインベントリ](https://doc.sitecore.com/xp/ja/developers/102/sitecore-experience-commerce/inventory-service.html "Inventory service")エンティティビューおよびアクションAPIを使って、XCインベントリの運用とデータを外部のビジネスシステムと統合できます。

> !注ビューズ&アクションAPIは、ビジネスユーザーインターフェースに対応するために設計された [オーサリングAPI](https://doc.sitecore.com/xp/ja/developers/102/sitecore-experience-commerce/authoring-api.html "Authoring API")であり、統合シナリオには最適化されていません。
> 
> Views and Actions APIをコマース統合で使用する際には、以下の点を考慮する必要があります。
> 
> -   Views and Actions APIは一度に1つのCommerceエンティティしか処理できず、バッチ処理はサポートしていません。
>     
> -   Views and Actions APIはCommerce WebサービスのAPIよりも多くのオーバーヘッドを伴います。Views and Action APIの呼び出しは通常3ステップで、アクションのビューを取得し、ビューのプロパティを更新した値で修正し、アクションを実行します。
>     

## インベントリセット(C#)を手に入れましょう

インベントリセットはプログラム的に取得できます。クエリはインベントリセットのIDを指定する必要があります(inventorySetId)。

C#を使ってインベントリセットを作成するには:

DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-InventorySet-{Id}", "Master", string.Empty, string.Empty);
EntityView view = Proxy.GetValue(query)
## インベントリセットの作成(C#)

インベントリセットはプログラム的に作成できます。

新しいインベントリセットを作成するには、例えばC#を使う:

1.  アクションのエンティティビューを取得できます:
    
    DataServiceQuerySingle<EntityView> query = container.GetEntityView(string.empty, "Details", "AddInventorySet", string.Empty);
    EntityView view = Proxy.GetValue(query);
    
2.  インベントリセットの詳細を指定してください。リクエストには新しいインベントリセットの名前(name)、表示名(DisplayName))、そしてオプションで説明(description)を指定しなければなりません。
    
    var Name = view.Properties.FirstOrDefault(p => p.Name.Equals("Name"));
    var DisplayName = view.Properties.FirstOrDefault(p => p.Name.Equals("DisplayName"));
    var Description = view.Properties.FirstOrDefault(p => p.Name.Equals("Description"));
    
    Name.Value = "NewSet";
    DisplayName.Value = "A New Inventory Set";
    Description.Value = "A brand new Inventory Set";
    
3.  行動を実行する:
    
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));
## インベントリセットの更新(C#)

既存のインベントリセットの詳細を編集できます。

インベントリセットの詳細を更新するには、例えば表示名や説明を変更するためにC#を用いてください:

1.  エンティティビューを入手:
    
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-InventorySet-{InventoryId}", "Details", "EditInventorySet", string.Empty);
    EntityView view = Proxy.GetValue(query);
    
2.  インベントリセットの最新情報を提供します:
    
    var Description = view.Properties.FirstOrDefault(p => p.Name.Equals("Description"));
    var DisplayName = view.Properties.FirstOrDefault(p => p.Name.Equals("DisplayName"));
    
    Description.Value = "A new Updated description"
    DisplayName.Value = "Changed Display Name";
    
3.  行動を実行する:
    
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));
## アイテムをインベントリセット(C#)に関連付ける

販売可能なアイテムを指定されたインベントリセットに関連付けることができます。例えば、販売可能なアイテムが2つのインベントリセットに関連付けられている場合、販売可能なアイテムを100ユニットインベントリセットAに、40ユニットをインベントリセットBに割り当てることができます。これにより、必要に応じて在庫を管理できます。

販売可能なアイテムを在庫セットに関連付けるには、例えばC#を使う:

1.  エンティティビューを入手:
    
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"",string.Empty,"DeleteCatalog", "Entity-Catalog-{CatalogId}");
    EntityView view = Proxy.GetValue(query);
    
2.  販売可能な品目の在庫情報を設定します。例えば:
    
    var SellableItem = view.Properties.FirstOrDefault(p => p.Name.Equals("SellableItem"));
    // System.String
    var Quantity = view.Properties.FirstOrDefault(p => p.Name.Equals("Quantity"));
    // System.Int32
    var InvoiceUnitPrice = view.Properties.FirstOrDefault(p => p.Name.Equals("InvoiceUnitPrice"));
    // System.Decimal
    var InvoiceUnitPriceCurrency = view.Properties.FirstOrDefault(p => p.Name.Equals("InvoiceUnitPriceCurrency"));
    // System.String
    var Preorderable = view.Properties.FirstOrDefault(p => p.Name.Equals("Preorderable"));
     // System.Boolean
    var PreorderAvailabilityDate = view.Properties.FirstOrDefault(p => p.Name.Equals("PreorderAvailabilityDate"));
    // System.DateTimeOffset
    var PreorderLimit = view.Properties.FirstOrDefault(p => p.Name.Equals("PreorderLimit"));
    // System.Int32
    var Backorderable = view.Properties.FirstOrDefault(p => p.Name.Equals("Backorderable"));
    // System.Boolean
    var BackorderAvailabilityDate = view.Properties.FirstOrDefault(p => p.Name.Equals("BackorderAvailabilityDate"));
    // System.DateTimeOffset
    var BackorderLimit = view.Properties.FirstOrDefault(p => p.Name.Equals("BackorderLimit"));
    // System.Int32
    
     SellableItem.Value = $"Entity-SellableItem-{ItemToAssociate}";
     Quantity.Value = 10;
     InvoiceUnitPrice.Value = 20.00;
     InvoiceUnitPriceCurrency.Value = "USD";
     Preoderable.Valu = true;
     PreOrderAvailabilityDate.Value = DateTimeOffset.Now;
     PreorderLimit.Value = 2;
     Backorderable.Value = false;
     BackorderAvailabilityDate.Value = DateTimeOffset.Now;
     BackorderLimit.Value = 0;
    
3.  行動を実行する:
    
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));
## インベントリセットからアイテムを切り離す(C#)

インベントリセットから販売可能なアイテム(sellableItemId)や特定のバリエーション(variationId)を削除できます。

例えば、販売可能な品目を指定された在庫セットから切り離すには:

1.  エンティティビューを取得し、販売可能なアイテムをアソシエイト解除する:
    
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-InventorySet-{InventorySetId}",string.Empty,"DisassociateSellableItemFromInventorySet", "Entity-SellableItem-{SellableItemId}");
    EntityView view = Proxy.GetValue(query);
    
2.  行動を実行する:
    
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));
## インベントリ移管(C#)

販売可能なアイテムの単位数を複数の在庫セット間で移すことができます。例えば、ある販売可能なアイテムを100単位在庫セットAに、40ユニットをセットBに割り当てたが、セットBが不足している場合、在庫セットAから在庫セットBへ追加の販売可能なアイテムを移すことができます。これにより、店舗や流通センターの異なるニーズに対応できます。

例えば、C#を使って、あるセットから別のセットへ在庫を移す場合:

1.  エンティティビューを入手:
    
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-InventorySet-{InventoryId}", "TransferInventory", "TransferInventory", $"Entity-SellableItem-{SellableItemId}");
    EntityView view = Proxy.GetValue(query);DataServiceQuerySingle<EntityView>; query = container.GetEntityView($"Entity-Category-{CategoryId}", "Details", "EditCategory", string.Empty);
    EntityView view = Proxy.GetValue(query);
    
2.  移転する在庫セットと数量を指定してください:
    
    var InventorySet = view.Properties.FirstOrDefault(p => p.Name.Equals("InventorySet"));
    // System.String
    var Quantity = view.Properties.FirstOrDefault(p => p.Name.Equals("Quantity"));
    // System.Int32
    InventorySet.Value = $"Entity-InventorySet-{InventorySetToPullFrom}";
    Quantity.Value = 10;
    
3.  行動を実行する:
    
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));
## カタログを在庫セット(C#)に関連付ける

カタログをインベントリセットに関連付けることができます。

例えば、カタログを在庫セットに関連付けるには、C#を用いてください:

1.  エンティティビューを取得し、カタログを削除してください:
    
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-InventorySet-{InventoryId}", "InventorySetCatalogs", "AssociateCatalog", string.Empty);
    EntityView view = Proxy.GetValue(query);DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-{ParentType}-{ParentId}",string.Empty,"DeleteCategory","Entity-Category-{CategoryId}");
    EntityView view = Proxy.GetValue(query);
    
2.  在庫セットに関連付けるカタログを指定します:
    
    var Catalog = view.Properties.FirstOrDefault(p => p.Name.Equals("CatalogName"));
    // System.String
    Catalog.Value = $"Entity-Catalog-{CatalogId}";
    
3.  行動を実行する:
    
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));
## カタログをインベントリセット(C#)から切り離す

カタログをインベントリセットから切り離すことができます。

カタログを在庫セットから分離するには、例えばC#を使って:

1.  エンティティビューを取得し、解除するカタログIDを指定・指定してください:
    
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-InventorySet-{InventoryId}", string.Empty, "DisassociateCatalog", {CatalogIdToDisassociate});
    
2.  行動を実行する:
    
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));