4

I am using Sitecore Commerce Experience 9 update-2, and want to add a custom child component to an entity.

The Sitecore.Commerce.Core.Component has a Key annotation (of type System.ComponentModel.DataAnnotations).

namespace Sitecore.Commerce.Core
{
  public class Component
  {
    ...

    [Key]
    public string Id { get; set; }

    ..
  }
}

This key annotation makes OData to generate a EntityType for components (or sub-classes of Component, e.g. CustomComponent).

<EntityType Name="CustomComponent" BaseType="Sitecore.Commerce.Core.Component">
    <Property Name="FirstName" Type="Edm.String" />
</EntityType>

OData generates a NavigationProperty for entity types, for example:

<NavigationProperty Name="Components" Type="Collection(CustomComponent)" />

Navigation properties are by default not handled by the OData server. Result of this is that child components (ChildComponents property on the Component class) cannot be easily added using the OOTB command controllers.

Only solution found so far is to implement a custom controller and method that adds a child component for a certain entity (e.g. an AddChildComponentToCart CommandController method).

Are there any other (cleaner, simpler) ways to add child components to an entity?

Any good reasons why a Component is not an OData complex type (this could be achieved by not having the [Key] annotation on the Component class)?

1

I passed this over to the Product Team to comment on this, and the response was.....

There is not need for a custom controller, all components and child components can be expanded using odata expand option, e.g.

{{ServiceHost}}/{{ShopsApi}}/Carts('Cart01')?$expand=Lines($expand=CartLineComponents),Components

{{ServiceHost}}/{{ShopsApi}}/SellableItems('Habitat_Master,6042134,')?$expand=Components($expand=ChildComponents($expand=ChildComponents($expand=ChildComponents($expand=ChildComponents))))
| improve this answer | |
  • Tried that, but the serializer only de-serializes the base Component class for child components. Any clues how to solve that? – Joost Nov 26 '18 at 13:55
  • Sitecore Support confirmed that the current approach for adding a custom component is to add your our Controller (and Block when necessary). Sadly not such a scalable approach. – Joost Dec 6 '18 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.