Work with the sellable items Entity Actions and Views API

Version: 10.3

Sitecore Experience Commerce can exchange product-related data with other business systems using the Catalog Entity Views and Actions API.

Note

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#:

RequestResponse
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#:

  1. Get the entity view:

    RequestResponse
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-Catalog-{Catalog}", "Details", "AddSellableItem", string.Empty);
    EntityView view = Proxy.GetValue(query);
  2. Specify the sellable item properties:

    RequestResponse
    var 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";
    
  3. Execute the action:

    RequestResponse
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));

Delete a sellable item (C#)

The following example shows how to delete a sellable item using C#:

  1. Get the entity view: 

    RequestResponse
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-Category-{Category}",string.Empty, "DeleteSellableItem", "Entity-SellableItem-{SellableItemId}");
    EntityView view = Proxy.GetValue(query);
  2. Delete the sellable item:

    RequestResponse
    var Action = view.Properties.FirstOrDefault(p => p.Name.Equals("DeleteOption")); action.Value = "Delete";
  3. Execute the action:

    RequestResponse
    CommerceCommand 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#:

  1. Get the entity view:

    RequestResponse
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-SellableItem-{SellableItemId}", "Details", "EditSellableItemDetails", string.Empty);
    EntityView view = Proxy.GetValue(query);
    	
  2. Modify the view's property with the details to update:

    RequestResponse
    var 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']";
  3. Execute the action:

    RequestResponse
    CommerceCommand 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#:

  1. Get the entity view:

    RequestResponse
    DataServiceQuerySingle<EntityView> query = container.GetEntityView($"Entity-Category-{Category}",string.Empty, "DeleteSellableItem", "Entity-SellableItem-{SellableItemId}");
    EntityView view = Proxy.GetValue(query);
  2. Disassociate the sellable item:

    RequestResponse
    var Action = view.Properties.FirstOrDefault(p => p.Name.Equals("DeleteOption")); action.Value = "Disassociate";
  3. Execute the action:

    RequestResponse
    CommerceCommand command = Proxy.DoCommand(container.DoAction(view));

Do you have some feedback for us?

If you have suggestions for improving this article,