Work with the sellable items Entity Actions and Views API
Sitecore Experience Commerce can exchange product-related data with other business systems using the Catalog Entity Views and Actions API.
The Views and Actions API is an authoring API designed to service a business user interface and is not optimized for integration scenario.
You should consider the following when using the Views and Actions API for your commerce integration:
-
The Views and Actions API can only process one Commerce entity at the time and does not support batch processing.
-
The Views and Actions API carries more overhead than the Commerce web service API. A call to the Views and Action API is typically a 3-step process that requires getting the action's view, modifying the view's properties with updated values, and then executing the action.
Get a sellable item (C#)
The following example shows how to get a sellable item using C#:
DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-SellableItem-{SellableItemId}", "Details", string.Empty, string.Empty); EntityView view = Proxy.GetValue(query);Create a sellable item (C#)
The following example shows how to create a sellable item using C#:
-
Get the entity view:
RequestResponseDataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-Catalog-{Catalog}", "Details", "AddSellableItem", string.Empty); EntityView view = Proxy.GetValue(query); -
Specify the sellable item properties:
RequestResponsevar ProductId = view.Properties.FirstOrDefault(p => p.Name.Equals("ProductId")); 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")); var Brand = view.Properties.FirstOrDefault(p => p.Name.Equals("Brand")); var Manufacturer = view.Properties.FirstOrDefault(p => p.Name.Equals("Manufacturer")); var TypeOfGood = view.Properties.FirstOrDefault(p => p.Name.Equals("TypeOfGood")); ProductId.Value = "SomeProductId".ProposeValidId(); //ProposeValidId will try to convert your string to a safe string to use. Name.Value = "AHotProduct"; DisplayName.Value = "A Hot New Product"; Description.Value = "The hottest product on the market today"; Brand.Value = "Hot Stuff Brands"; Manufacturer.Value = "Hot Stuff Manufacturers"; TypeOfGood.Value = "Physical Item"; -
Execute the action:
RequestResponseCommerceCommand command = Proxy.DoCommand(container.DoAction(view));
Delete a sellable item (C#)
The following example shows how to delete a sellable item using C#:
-
Get the entity view:
RequestResponseDataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-Category-{Category}",string.Empty, "DeleteSellableItem", "Entity-SellableItem-{SellableItemId}"); EntityView view = Proxy.GetValue(query); -
Delete the sellable item:
RequestResponsevar Action = view.Properties.FirstOrDefault(p => p.Name.Equals("DeleteOption")); action.Value = "Delete"; -
Execute the action:
RequestResponseCommerceCommand command = Proxy.DoCommand(container.DoAction(view));
Update a sellable item (C#)
The following example shows how you can use the Entity Views and Actions API to update the details of a sellable item using C#:
-
Get the entity view:
RequestResponseDataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-SellableItem-{SellableItemId}", "Details", "EditSellableItemDetails", string.Empty); EntityView view = Proxy.GetValue(query); -
Modify the view's property with the details to update:
RequestResponsevar displayNameProperty = view.Properties.FirstOrDefault(p => p.Name.Equals("DisplayName")); displayNameProperty.Value = "Edited Display Name"; var descriptionProperty = view.Properties.FirstOrDefault(p => p.Name.Equals("Description")); descriptionProperty.Value = "Edited Description"; var brandProperty = view.Properties.FirstOrDefault(p => p.Name.Equals("Brand")); brandProperty.Value = "Edited Brand"; var manufacturerProperty = view.Properties.FirstOrDefault(p => p.Name.Equals("Manufacturer")); manufacturerProperty.Value = "Edited Manufacturer"; var typeOfGoodProperty = view.Properties.FirstOrDefault(p => p.Name.Equals("TypeOfGood")); typeOfGoodProperty.Value = "Edited TypeOfGood"; var tagsProperty = view.Properties.FirstOrDefault(p => p.Name.Equals("Tags")); typeOfGoodProperty.Value = "['Tag1', 'Tag2', 'Tag3']"; -
Execute the action:
RequestResponseCommerceCommand command = Proxy.DoCommand(container.DoAction(view));
Disassociate a sellable item (C#)
The following example shows how you can use the Entity Views and Actions API to disassociate a sellable item (for example, from a category), using C#:
-
Get the entity view:
RequestResponseDataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-Category-{Category}",string.Empty, "DeleteSellableItem", "Entity-SellableItem-{SellableItemId}"); EntityView view = Proxy.GetValue(query); -
Disassociate the sellable item:
RequestResponsevar Action = view.Properties.FirstOrDefault(p => p.Name.Equals("DeleteOption")); action.Value = "Disassociate"; -
Execute the action:
RequestResponseCommerceCommand command = Proxy.DoCommand(container.DoAction(view));