How To Import Catalog Items Into Episerver Commerce Using The Catalog Import

If you want to import products/variants/catalog nodes into EPiserver commerce then one option that will be of interest to you will be the CatalogImportExport -> Import() method within the Mediachase.Commerce assembly.

A common example of when you may want to do this is when you're working with third-party PIM product like, InRiver, e.g. you want to make changes within your PIM and that process involves an out-the-box-connector that gets pulled into commerce. In this post, I'm hopefully going to cover some of my learnings while attempting to import items into commerce.

Before I go too far into the code, I should point out a perfectly valid way of importing is to get the XML file, parse out the bits you want and then use the normal commerce API to import objects.  I think this is still my preferred approach.  You probably end up with more code, but, as you're writing all the code yourself, it's easier to unit test and debug when things go wrong. 

I'm going to be honest here, getting the import process fully working so things like markets, languages was a pain.  If you're pulling in data within an import and you don't have an explicit property mapped in your commerce objects you can inadvertently end up with hidden properties in your products and variants that you can't see in your code.

Getting all the data 100% working, with no import issues can be tricky. One of the big problems being the sheer amount of data you need to keep in sync. I've had to do this on a recent project and figuring out what goes where, where things were stored etc..  required a lot of time and patience. If you're just about to go through this process yourself, then give yourself plenty of time, it'd usually be better to over-estimate than underestimate :)

How Do I Import Code?


        var path = "C:\\ImportLocation\\Import.xml";
        var xmlStream = File.OpenRead(path);
        public void ImportIntoCommerce(FileStream fileStream)
            var baseFilePath = string.Empty;
            var overwrite = false;

            var cie = new CatalogImportExport();
            cie.ImportExportProgressMessage += ProgressHandler;
            cie.Import(fileStream, Mediachase.Commerce.Core.AppContext.Current.ApplicationId, baseFilePath, overwrite);

        private void ProgressHandler(object source, ImportExportEventArgs args)
            // Do some logging.  Althought this can slow down the import a bit.


The code is, hopefully, fairly straight-forward to understand.  I'm reading an export file stored on my local disc, this gets serializing into a file stream and then passed into the Import() method.  As long as you have a valid commerce export file this should import correctly. 

Import File Overview For InRiver

This section doesn't have anything to do with importing, but it might be handy for me to quickly discuss my learnings surrounding the import file, as no one else seems to have done so.

FIrstly, I should probably say I've only used this importing method once with an InRiver integration, which used the standard connector so I'm not sure how this relates to other types of import files generated for commerce.

InRiver generated a folder, containing an xml file and a zip file that contained all the changes for a given PIM amend. The export folder contains a file called catalog.xml and a ZIP file.

The zip file contains additional catalog.xml file that contains slightly different data which is also used in the import. When I first tried implementing this, the extra XML file within the ZIP threw me a bit (as I didn't look inside it). For the record it contains data that will be imported, e.g. things like meta-data, drop-down entries etc.. In my example with InRiver, the names of the properties being imported were contained within the MetaField -> Name. The type can be found within the Type file. If you don't have a matching property on your EPiserver product/variant class, when the import runs, any types defined will be imported as effectively hidden fields. If you bump into an issue of missing data within your C# code, you can try the method below to double check that the property exists.

How Do I Access These Hidden Properties?

If you are new to commerce and you want to know how to access a 'hidden' property, this can be done using this:

            MyVariant variant = null;
            var reference = new ContentReference(1);
            if (_contentRepository.TryGet(reference, out variant))
               var data = variant["MyProperty"] = excludedMarketsValue;

Importing Takeaway

As you can see, importing a file into commerce is simple. In most situations, it's the tasks of making sure the right things have imported, making sure meta-data has been added correctly etc... is the hard part. I think I'd still recommend manually writing the code to map your import file, as in the long run, it will be easier to maintain and debug. If your import does dodgy things and all the code is within internal Episerver assemblies, it's harder to figure out exactly what's going on.

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top