Sitecore Commerce 9: How to Export a CSV File from a Pipeline Block

Andrei Paliakou on December 9, 2020
sitecore-techtalks
Few days ago, I saw a question on Sitecore #commerce Slack channel. The question is:

Any idea on how we can forcefully download a csv file from a pipeline block?

In my previous blog post, I explained how to export Price Books. In case of Price Books, there was exported data in zip extension. Therefore, it is not a big deal to implement the export of any custom data from Sitecore Commerce in csv format. I have implemented a sample plugin and in the current blog post will share my knowledge how I did it.

The source code you can find here: https://github.com/Frog911/Plugin.Sample.Commerce.ExportCsv

First of all, you have to add reference to CsvHelper NuGet package.

Then we have to create IExportDataPipeline pipeline:

Sitecore Commerce 9: Export csv

Sitecore Commerce 9: Export csv

For this pipeline, we have to create ExportDataBlock:

Sitecore Commerce 9: Export csv

Here we are using IStreamDataToCsvPipeline, which we also have to implement.

Sitecore Commerce 9: Export csv

Sitecore Commerce 9: Export csv

IStreamDataToCsvPipeline will contain StreamDataToCsvBlock, which will be responsible to build csv file for download.

Sitecore Commerce 9: Export csv

And, of course, we have to register our pipelines.

Sitecore Commerce 9: Export csv

How we are ready to use these pipelines in any custom endpoint and we can use Postman to test these logic.

Sitecore Commerce 9: Export csv

 

I hope this blog post will be useful for someone who wants to implement such export data feature.

Stay tuned!