Episerver Commerce R2 Automated Csv Import

Vote:
 

Hi

I have recently created the necessary csv files to import products into the episerver database. All works fine and I like the csv format for creating the products as compared to writing the whole code to import the products. I need to automate the process. Basically all I need to do is to create a file watcher and as soon a file is dropped in the folder it imports the file into the db. What I wanted to know is that whether there is a way to import the csv files in code using a web service or as API. I need to import all 8 type of csv files provided by episerver. Any help is appreciated.

Thanks

Kind Regards

Sandeep

 

#51681
Jun 21, 2011 1:40
Vote:
 

Hi Sandeep,

I had to import products myself as well, but also needed to import other information (like for instance customer information).
Our solution therefore didn't use the .csv import and I never got around to see if this way of importing could be automated.

However, the solution we did use works very well.
We now use an EPiServer job that is scheduled to run and collect data from a non EPiServer database. This data is then imported into the EPiserver Commerce database by using the EPiServer Commerce dto objects.
The job handles all kinds of stuff (productinformation, pricing, customers, organisations) and all works fine.

An example for importing catalogs (categories and entries are imported afterwards):

        public static int CreateCatalog(Catalog catalog)
        {
            CatalogDto catalogDto = CatalogContext.Current.GetCatalogDto(-1, new CatalogResponseGroup(CatalogResponseGroup.ResponseGroup.CatalogInfo));

            CatalogDto.CatalogRow newCatalogRow = catalogDto.Catalog.NewCatalogRow();
            Mapper.CatalogRowMapper(catalog, newCatalogRow);
            catalogDto.Catalog.AddCatalogRow(newCatalogRow);
            CatalogContext.Current.SaveCatalog(catalogDto);

            return catalogDto.Catalog[0].CatalogId;
        }

    

#51793
Edited, Jun 23, 2011 10:01
Vote:
 

Hi,

In order to import CSV file, you need to have the mapping file as well. You can take a look at the EPiServer Commerce Webhelp: http://webhelp.episerver.com/Commerce/1.1/EN/user/Default.htm

Navigate to Commerce Manager > Catalog Management > Working with Catalogs > Using CSV Files to Quickly Create Custom Catalogs

You can:

1. Go to Commerce Manager and Catalog CSV Import

2. Upload your CSV file and mapping

3. Save mapping file to your solution.

4. Next time you can use CSV and Mapping file to import

        private void ImportCatalogCsvFile(string csvFilePath, string mappingFilePath)
        {
            Rule rule = Rule.XmlDeserialize(CatalogContext.MetaDataContext, mappingFilePath);
            char textQualifier = '\0';
            if (!string.IsNullOrEmpty(rule.Attribute["TextQualifier"]))
            {
                textQualifier = char.Parse(rule.Attribute["TextQualifier"]);
            }

            string encodingAtt = string.IsNullOrEmpty(rule.Attribute["Encoding"]) ? "Default" : rule.Attribute["Encoding"];
            Encoding encoding = Encoding.Default;
            if (!string.IsNullOrEmpty(encodingAtt) && encodingAtt.Equals("Default", StringComparison.OrdinalIgnoreCase))
            {
                encoding = Encoding.GetEncoding(encodingAtt);
            }

            var sourcePath = HttpContext.Current.Server.MapPath(ManagementHelper.GetImportExportFolderPath("csv/data"));
            IIncomingDataParser dataParser = new CsvIncomingDataParser(sourcePath, true, char.Parse(rule.Attribute["Delimiter"]), textQualifier, true, encoding);
            DataSet dataSet = dataParser.Parse(Path.GetFileName(csvFilePath), null);
            DataTable rawData = dataSet.Tables[0];
            int catalogId = 1;
            FillResult fillResult = null;
            string typeName;
            if ((typeName = rule.Attribute["TypeName"]) != null)
            {
                switch (typeName)
                {
                    case "Category":
                        {
                            CatalogContext.MetaDataContext.UseCurrentUICulture = false;
                            CatalogContext.MetaDataContext.Language = rule.Attribute["Language"];
                            MappingMetaClass mappingMetaClass = new CategoryMappingMetaClass(CatalogContext.MetaDataContext, rule.ClassName, catalogId);
                            fillResult = mappingMetaClass.FillData(FillDataMode.All, rawData, rule, -1, DateTime.UtcNow);
                            CatalogContext.MetaDataContext.UseCurrentUICulture = true;
                        }
                        break;
                    case "Entry":
                        {
                            CatalogContext.MetaDataContext.UseCurrentUICulture = false;
                            CatalogContext.MetaDataContext.Language = rule.Attribute["Language"];
                            MappingMetaClass mappingMetaClass = new EntryMappingMetaClass(CatalogContext.MetaDataContext, rule.ClassName, catalogId);
                            fillResult = mappingMetaClass.FillData(FillDataMode.All, rawData, rule, -1, DateTime.UtcNow);
                            CatalogContext.MetaDataContext.UseCurrentUICulture = true;
                        }
                        break;
                    case "EntryRelation":
                        {
                            MappingMetaClass mappingMetaClass = new EntryRelationMappingMetaClass(CatalogContext.MetaDataContext, catalogId);
                            fillResult = mappingMetaClass.FillData(FillDataMode.All, rawData, rule, -1, DateTime.UtcNow);
                        }
                        break;
                    case "EntryAssociation":
                        {
                            MappingMetaClass mappingMetaClass = new EntryAssociationMappingMetaClass(CatalogContext.MetaDataContext, catalogId);
                            fillResult = mappingMetaClass.FillData(FillDataMode.All, rawData, rule, -1, DateTime.UtcNow);
                        }
                        break;
                    case "Variation":
                        {
                            MappingMetaClass mappingMetaClass = new VariationMappingMetaClass(CatalogContext.MetaDataContext, catalogId);
                            fillResult = mappingMetaClass.FillData(FillDataMode.All, rawData, rule, -1, DateTime.UtcNow);
                        }
                        break;
                    case "SalePrice":
                        {
                            MappingMetaClass mappingMetaClass = new PricingMappingMetaClass(CatalogContext.MetaDataContext, catalogId);
                            fillResult = mappingMetaClass.FillData(FillDataMode.All, rawData, rule, -1, DateTime.UtcNow);
                        }
                        break;
                }
            }

            if (fillResult != null)
            {
                if (fillResult.ErrorRows > 0)
                {
                    Exception[] exceptions = fillResult.Exceptions;
                    foreach (Exception ex2 in exceptions)
                    {
                        string text = ex2.Message;
                        if (ex2 is MDPImportException)
                        {
                            var ex3 = (MDPImportException)ex2;
                            if (ex3.RowIndex > -1)
                            {
                                text = string.Format("Import error. Line {0}: {1}", ex3.RowIndex + 1, text);
                            }
                        }
                        Console.WriteLine(text);
                    }
                }
                if (fillResult.Warnings.Length > 0)
                {
                    MDPImportWarning[] warnings = fillResult.Warnings;
                    foreach (MDPImportWarning warning in warnings)
                    {
                        Console.WriteLine(string.Format("Line {0}: {1}", warning.RowIndex + 1, warning.Message));
                    }
                }
                Console.WriteLine(string.Format("Succeed: {0} in Total {1}", fillResult.SuccessfulRows, fillResult.TotalRows));
            }
        }

    

#54131
Oct 04, 2011 9:05
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.