Import
Overview
The DataImporter provides batches of data that are imported by an IEntityImporter implementation. Import profiles are entities that bind the import to an ImportEntityType and combine and store all aspects of an import, such as import field mappings and settings. It makes them configurable by the user.
When an import is executed, a task associated with the import profile is started, to perform the actual import via the DataImporter and the IEntityImporter. The task can be scheduled using cron expressions or triggered manually.
Data importer
The DataImporter is an IDataImporter implementation designed to provide batches of import data to IEntityImporter implementations in a high-performance way.
Events
The three published events surrounding the data import are:
ImportExecutingEvent: published before a data import. It can be used to load custom data into the context object, that must be available throughout the import.
ImportBatchExecutedEvent: published by the
IEntityImporterafter it has imported a batch of data. It can be used to import the data of newly attached columns, data theIEntityImporterdoes not know about.ImportExecutedEvent: published after a data import. It can be used, for example, to remove data from the cache so that the next time it is accessed the imported data is considered.
Import profile
Import profiles combine all aspects of an import to make it configurable by the user: import file(s), key fields to identify existing records, CSV configuration, and import field mappings. You can use the IImportProfileService to manage import profiles, for example to get a list of import files associated with a profile.
Entity importer
An import is achieved via an IEntityImporter implementation or can inherit from EntityImporterBase. This provides helper methods that can be used by all importers, like importing localized properties.
ProcessBatchAsync is the main method provided by EntityImporterBase to import data. It is called several times by the data importer during an import (once for each data batch). The importer is rebuilt for each batch via its own dependency scope. This ensures that there are no unwanted interactions with the scope of the data importer.
For more sample code, see the core's built-in importers such as ProductImporter, CategoryImporter etc.
Avoid reloading the same data for each batch. Use ImportExecuteContext.CustomProperties to store extra data that needs to be available during the entire import. You can load them once using the ImportExecutingEvent or a helper method like:
Media importer
The media importer IMediaImporter is a helper for importing media files like images. It can and should be used by any importer. Its purpose is to download files if required and to avoid importing duplicate files by comparing their binary contents. It also uses IMediaService.BatchSaveFilesAsync to save files in an efficient way.
Importing new images may result in duplicates if the TinyImage module is installed or the images are larger than the Maximum image size media setting.
Custom import via module
There is no provider mechanism available for data imports like in the export infrastructure. So you cannot simply bind your custom importer to an import profile. This would require a core extension. To realise a custom import via a module, you have to provide an importer from scratch and a task that calls it directly. First, implement your importer and register it:
Then add a task that runs your importer:
Use the MinimalTaskViewComponent if you want to execute your importer from a view of your module (e.g. a configuration page).
Last updated
Was this helpful?