Creating an Export provider

Export providers allow you to export your store data in many different formats. Smartstore mainly uses CSV and XML. In this tutorial you will write an export provider for the product catalog.

To learn more about exporting, see Export.

Create a configuration

If you want make your export provider customizable, you will need to add some resources. This step is optional. There are three things to configure:

  1. The ProfileConfigurationModel that describes the configurable data.

  2. The HelloWorldConfigurationViewComponent, which converts stored data into usable formats for your view.

  3. The view shown to the user. This acts like a widget view and must adhere to the same directory structure.

This configuration allows you to limit the number of rows that are exported.

The Model

Create the ProfileConfigurationModel.cs class and add it to the Models directory.

namespace MyOrg.HelloWorld.Models
{
    [Serializable, CustomModelPart]
    [LocalizedDisplay("Plugins.MyOrg.HelloWorld.")]
    public class ProfileConfigurationModel
    {
        [LocalizedDisplay("*NumberOfExportedRows")]
        public int NumberOfExportedRows { get; set; } = 10;
    }
}

Add the resources to your localization file.

The ViewComponent

Create the HelloWorldConfigurationViewComponent.cs class and add it to the Components directory.

The View

Create a Razor view Default.cshtml and add it to Views / Shared / Components / HelloWorldConfiguration.

Add export providers

The CSV export provider

Create the HelloWorldCsvExportProvider.cs class and add it to the new Providers directory.

This class inherits from the abstract base class ExportProviderBase, so you must override the ExportAsync method.

Add Attributes

Smartstore uses the following attributes to correctly integrate the providers: SystemName, FriendlyName and the optional ExportFeatures.

Add these attributes to your class definition.

SystemName is the system name of the provider, not the system name of the module.

Export feature
Description

CreatesInitialPublicDeployment

Automatically create a file-based public deployment when an export profile is created.

CanOmitGroupedProducts

Offer option to include/exclude grouped products.

CanProjectAttributeCombinations

Provide the ability to export attribute combinations as products.

CanProjectDescription

Offer more options to manipulate the product description.

OffersBrandFallback

Offer the option to enter a brand fallback.

CanIncludeMainPicture

Provide an option to set an image size and get the URL of the main image.

UsesSkuAsMpnFallback

Use SKU as manufacturer part number if MPN is blank.

OffersShippingTimeFallback

Provide an option to enter a shipping time fallback.

OffersShippingCostsFallback

Offer the option to enter a shipping cost fallback and a free shipping threshold.

CanOmitCompletionMail

Automatically send a completion email.

UsesAttributeCombination

Provide additional data for attribute combinations.

UsesAttributeCombinationParent

Export attribute combinations as products, including the parent product. This is only effective in conjunction with the CanProjectAttributeCombinations export feature.

UsesRelatedDataUnits

Provide additional data units for related data.

To tell the provider that you want to export a CSV file, override the FileExtension property. The Localizer is used to localize messages. CsvConfiguration specifies CSV format details.

Add configuration

Next, you need to tell the provider how it should be configured (ViewComponent and Model). This is done with the ConfigurationInfo method.

Export data

You are now ready to begin exporting your data. Going back to the ExportAsync method, start by retrieving the profile configuration data.

Get a CsvWriter and write the first row of column names.

Now iterate over the data segments.

Inside the while loop, we get the product and it's entity.

The difference between Entity and Product is as follows:

  • Entity: Represents the original entity read from the database.

  • Product: A dynamic object that encapsulates the entity and enriches it with computed data.

Add a try-catch block for error handling.

Now calculate the savings within the try block.

Next, we write the fields in the order of our columns. Then we increment the number of rows.

Finally, you want to limit your row exports to NumberOfExportedRows from the profile configuration data.

Your code could look like this:

The XML export provider

XML export is very similar to CSV export. First, you need to change the file extension to XML.

The Localizer and ConfigurationInfo stay the same. Now you just need to change ExportAsync. For XML, you get the writer from an ExportXmlHelper.

Next, you start the document and write the grouping tag.

As with the CSV provider, you fetch the next data segment, iterating through the products.

Write a new Product XML node with the values you want to export.

And when the while loop is complete, the grouping and the document must be closed.

The source code can be found in HelloWorldXmlExportProvider.cs.

Delete export profiles

Finally, you just need to clean up any existing profiles on UnistallAsync in Module.cs. Pass SmartDbContext and IExportProfileService to the module class constructor.

Then add the following lines to the top of your UninstallAsync method

Conclusion

In this tutorial, you created an export provider. You have created a configuration profile and a CSV export provider.

The code for this tutorial can be found in the examples repository.

Last updated

Was this helpful?