In my previous blog post, I explained how the Catalogs Export works under the hood. Now I'll show how the export process can be implemented for Price Books or any other custom entities in Sitecore Commerce.
You can find the source code here: https://github.com/Frog911/Sitecore.Commerce.ImportExport
First, we have to create ApiController and ExportPriceBooks action. The action will support several parameters like filename, mode, and maximumItemsPerFile.
Let’s create ExportPriceBooksCommand. The command will look like this:
We have to create a custom argument for the pipeline, which we will create a bit later.
ExportPriceBooksArgument will be used by pipeline and pipeline blocks to share some data through the blocks.
Now we can create our pipeline interface and implement this interface:
We have created a pipeline, so now we can focus on block implementation.
Let’s create ExportPriceBookBlock. The source code for this block should look like below:
As you can see, it is required to create a new IStreamPriceBooksToArchivePipeline and implement this interface.
Let’s create them!
Now we are going to create three custom blocks for this pipeline:
StreamPriceBooksToArchiveBlock is responsible to export PriceBook entities. We have to override the virtual method from the base class and set the type of entity, which we have to export. In this case, it is a PriceBook entity. You can use your custom entity type if you want to export your custom entities.
In the last block, we have to export PriceCards. The implementation will look like below:
We need to register the endpoint, which we have created at the beginning.
The last step is registering all pipelines and blocks in ConfigureSitecore.cs:
Now everything is ready for the test.
We can create a Postman request to call our custom endpoint.
The Price Book Export works fine. We are able to save the archive with the exported data.
In the next blog post, I will explain how to import this exported ZIP file to Sitecore Commerce.