Building Custom Data Importer¶
Follow the intructions below to build your own data importer of a specific type. They describe importing product images from a CSV file.
-
Сheck which components need to be implemented into a new importer:
-
Create a CSV file.
Let's say you have a CSV file to import your data from with two columns:
productId
img_url
In order to import multiple images for a single product, you need to use multiple rows with the same product ID.
Since only CSV format is currently supported for out-of-the-box import, there are two options to solve the issue:
- Create a CSV file in any preferred editor.
-
Use this sample:
-
Custom CSV mapping to use with default CsvDataReader.
There's no need to define any new custom reader because:
- A built-in CsvDataReader is used to read the data from the CSV file.
- It uses the CsvHelper library to read and parse data.
Define
ClassMap
by creating a new file CsvProductImageClassMap.cs with the following content:CsvProductImageClassMap.cs1 public class CsvProductImageClassMap : ClassMap<ProductImage> 2 { 3 public CsvProductImageClassMap() 4 { 5 Map(m => m.ProductId); 6 Map(m => m.ImageUrl); 7 } 8 }
This will map the class properties to the column headers of the CSV file. This example is identical to not using any class mapping at all when the headers match the property names. For more information, see Class Maps.
-
Define settings for custom importer. For any given import profile instance, each importer can provide settings that can be modified by the user. Define the settings that will be used by our importer in the ProductImageImporterSettings.cs file:
ProductImageImporterSettings.cs1 public class ProductImageImporterSettings 2 { 3 public static SettingDescriptor DebugSetting { get; } = new SettingDescriptor 4 { 5 Name = "Vcmp.Import.ProductImage.Debug", 6 ValueType = SettingValueType.Boolean, 7 GroupName = "Import", 8 DefaultValue = false 9 }; 10 11 public static IEnumerable<SettingDescriptor> AllSettings 12 { 13 get 14 { 15 yield return DebugSetting; 16 } 17 } 18 }
-
Create DataWriter in the new CsvProductImageWriter.cs file:
Line 6 gets a value for setting from the profile. This setting value can be provided by user for the particular import profile instance.
Line 13 creates a loop between passed objects of the
ProductImage
type. Inside this loop, you can add a piece of code that saves the passed object within the system.Line 31 notifies the one who runs the importer about any possible error when writing the data being imported through
context.ErrorCallback
. -
Create custom importer. This is the central object in the DataImport extension system, as the object of this type is called by the system for all import operations. To define a new importer, create CsvProductImageImporter with the following content:
Lines 23 and 34 return both the reader and writer and get executed by the import process manager.
-
Register data importer by adding the following content to the module.cs file:
Line 3 registers
CsvProductImageImporter
in the DI.Line 8 registers
CsvProductImageImporter
in the global importer registry, so that the new importer may become available for import profile creation and for running the import process. -
Run data importer:
To run data importer from the user interface as
ImportProfile
that has an assignedCsvProductImageImporter
:- Manually select a profile on the blade.
- Specify the file for import. The preview is availabe.
- Manually start the import process and observe the progress.
- Interrupt the import, if needed.
- See the summary information at the end.
The user gets all the information on the current import process and can control it.
To run data importer using API methods from the third-party code, for example, for integrations through LogicApps:
- Specifiy the profile to be used.
- Configure the path to the file.
- Trigger the import on a schedule (with the set parameters).
The import is managed automatically without user input.
Note
VC supports CSV for out-of-the-box import. You can create your own file reader to use a different format.
For more information, see ImportSampleModule.