How to Export Price Books in Sitecore Commerce 9

sitecore-techtalks

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.

Sitecore Commerce 9: Export Price Books

Let’s create ExportPriceBooksCommand. The command will look like this:

Sitecore Commerce 9: Export Price Books

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.

Sitecore Commerce 9: Export Price Books

Now we can create our pipeline interface and implement this interface:

Sitecore Commerce 9: Export Price Books

Sitecore Commerce 9: Export Price Books

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:

Sitecore Commerce 9: Export Price Books

As you can see, it is required to create a new IStreamPriceBooksToArchivePipeline and implement this interface.

Let’s create them!

Sitecore Commerce 9: Export Price Books

Sitecore Commerce 9: Export Price Books

Now we are going to create three custom blocks for this pipeline:

Sitecore Commerce 9: Export Price Books

 

Sitecore Commerce 9: Export Price Books

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:

Sitecore Commerce 9: Export Price Books

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:

Sitecore Commerce 9: Export Price Books

Sitecore Commerce 9: Export Price Books

Now everything is ready for the test.

We can create a Postman request to call our custom endpoint.

Sitecore Commerce 9: Export Price Books

 

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.

Stay tuned!