Sitecore Commerce 9: How to Import/Export Catalogs

sitecore-techtalks
Sitecore Experience Commerce (XC) 9 provides an API for importing/exporting catalogs, inventory sets, and environment configuration out-of-the-box. It enables developers to create structured commerce entities in the development environment or a UAT environment and then move them to production environment. You can do it via Postman requests, which Sitecore provides with Commerce Engine SDK. However, the person who will be responsible for migration must be familiar with Postman, because right now it is not possible to do that throw BizFX interface.

Price Book migration is also important part, which is missing for now in XC. I will share a few plugins in my blog post series, which will provide a possibility to import/export Price Books via Postman requests, import/export Catalogs, Inventory Sets and Price Books via BizFx interface. It will help editors of Sitecore Commerce who are responsible for these types of tasks and make their lives easier.

The source code: https://github.com/Frog911/Sitecore.Commerce.ImportExport

Sitecore Commerce 9: Export Catalogs Under the Hood

 

You have to call Get Token API to receive the token first, and then send this request to XC by clicking on Send and Download option. The Commerce API will generate ZIP file for you, which will contain all the catalogs data.

Let’s decompile DLLs and go through the endpoint implementation. You can find the endpoint implementation in Sitecore.Commerce.Plugin.Catalog.dll

In the ExportCatalogs action, endpoint extracts Request Body parameters and call ExportCatalogsCommand.

Sitecore Commerce 9: Export Catalogs Under the Hood

Inside the ExportCatalogCommand it calls IExportCatalogsPipeline with the parameters from the Request.

Sitecore Commerce 9: Export Catalogs Under the Hood

You can find out more about implementation of IExportCatalogsPipeline in the ConfigureSitecore.cs file.

There is only one block that was registered for this pipeline. It is the ExportCatalogsBlock.

Sitecore Commerce 9: Export Catalogs Under the Hood

If we navigate to ExportCatalogsBlock we will see that the Export Catalogs feature supports only FULL type of export mode. I guess in the next releases Sitecore Commerce product team will add more supported export modes. I would like to pay attention on the FileCallbackResult class. It provides a possibility to send to the response of a request zip archive.

Sitecore Commerce 9: Export Catalogs Under the Hood

The Exportfull method calls StreamCatalogItemsToArchivePipeline. This pipeline is responsible for exporting data preparation, which will be stored in the exported JSON file.

Sitecore Commerce 9: Export Catalogs Under the Hood

Let’s have a closer look into the StreamBulkSellableItemsToArchiveBlock. This block inherits StreamBulkCatalogItemsToArchiveBaseBlock and inside of Run method it invokes StreamCatalogItems method the inherited class.

Sitecore Commerce 9: Export Catalogs Under the Hood

StreamCatalogItems method retrieves entities from the Commerce Engine by ListName, extorts Localizations and Relationships.

If you extract a downloaded zip file with the exported data, you will see many JSON files there:

Sitecore Commerce 9: Export Catalogs Under the Hood

The file example of SellableItem.1.json file looks like below:

Sitecore Commerce 9: Export Catalogs Under the Hood

A similar approach is in the Export Inventory Sets. There is just export of another type of commerce entities. Since Sitecore Commerce does not provide a possibility to export Price Books, I will explain how you can implement your custom import/export endpoints for that. It is also possible to implement import/export for any custom types of commerce entities based on my example, but it will be required to adapt my logic with your custom entities.

I will explain how to implement import/export Price Book endpoints on top of Sitecore Commerce OOTB import/export pipelines in my next blog post.

Stay tuned!