Configuring your OData Services

Sitecore Experience Commerce and the Commerce Engine use the OData Protocol to describe the both the data and data model available within core and custom plugins. But to get this all working we need to register a pipeline block in the IConfigureServiceApiPipeline Pipeline that using the ODataConventionModelBuilder to describe the type system, relationships and the actions that can be exposed by the OData service model. So what do we need to do here? We need to register:

  • Entities
  • Models
  • Commands

This provides information on object being returned and commands we can execute.

[PipelineDisplayName("WishListConfigureServiceApiBlock")]
public class ConfigureServiceApiBlock : PipelineBlock<ODataConventionModelBuilder, ODataConventionModelBuilder, CommercePipelineExecutionContext>
{
    public override Task<ODataConventionModelBuilder> Run(ODataConventionModelBuilder modelBuilder, CommercePipelineExecutionContext context)
    {
        Condition.Requires(modelBuilder).IsNotNull($"{this.Name}: The argument cannot be null.");

        modelBuilder.AddEntityType(typeof(WishList));
        modelBuilder.AddComplexType(typeof(WishListLineAdded));
        modelBuilder.EntitySet<WishList>("WishLists");

        var addLineConfiguration = modelBuilder.Action("AddWishListLine");
        addLineConfiguration.Parameter<string>("wishListId");
        addLineConfiguration.Parameter<string>("itemId");
        addLineConfiguration.ReturnsFromEntitySet<CommerceCommand>("Commands");

        var removeLineConfiguration = modelBuilder.Action("RemoveWishListLine");
        removeLineConfiguration.Parameter<string>("wishListId");
        removeLineConfiguration.Parameter<string>("wishListLineId");
        removeLineConfiguration.ReturnsFromEntitySet<CommerceCommand>("Commands");

        return Task.FromResult(modelBuilder);
    }
}