Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Import images using CSV Catalog import in CommerceManager

Vote:
 

Hello

I'm using Commerce version 12 and looking at the Catalog import using CSV files and XML mapping files,

http://webhelp.episerver.com/latest/commerce/catalog-management/importing-and-exporting-a-catalog.htm

I'm having trouble getting images to be linked to the prodcuts / variants I import.

I see that the value I set in the file is saved in the DB, but the file itself is not uploaded, and the field value in the CMS editor is empty.

If I try and add an image using the editor, the field value saved in the DB is in a very different format

Example "~/link/8c3c922a7069460a91721964e3ed4d04.aspx"

  • Where should the images be placed, on local hard drive or already uploaded to the CMS?
  • How should I refer to the image in the import file?
#203184
Apr 10, 2019 18:08
Vote:
 
  1. Catalog import does not embed the images (technically it can but that is not recommended ). The recommended way is to upload the images to cms as content then link them to nodes/entries as assets
  2. If you have catalog with linked assets, those will not be imported in Commerce Manager as the content type for the asset would not present. You would have to write code to import the catalog in context of your front (i.e. Cms) site
#203185
Apr 10, 2019 18:24
Vote:
 

I read it too fast, and missed the "CSV" part. It's still true then, you can't upload the image files to CMS system in Commerce Manager. You have to upload them to CMS, get their GUIDs then link them to the products. It sounds like it would be easier to write a scheduled job to handle that for you. 

#203191
Apr 10, 2019 23:20
Vote:
 

Embedding images during the import is quite heavy and time-consuming process, consider that you have a large image set with different image groups for thumbnail, gallery... to upload. A practice is to implement a schedule job to handle that.

You could either place the image on a hard drive or upload it somewhere in the CMS, then in the schedule job you can load the image file from drive or CMS, perform the rezising ..., and upload it into Episerver. Below is some sample code we used to upload an images folder to Epi for CMS version 10 and Commerce version 11

        private void UploadImageIntoEpi(string imagesSourcePath, EPiServer.Core.ContentFolder uploadFolder)
        {
            if (!imagesSourcePath.IsNullOrWhiteSpace())
            {
                var fileEntries = System.IO.Directory.GetFiles(imagesSourcePath);
                foreach (var fileEntry in fileEntries)
                {
                    try
                    {
                        var existingMedia =
                            _contentRepository.GetChildren<MediaData>(uploadFolder.ContentLink)
                                .FirstOrDefault(x => x.Name.Equals(Path.GetFileName(fileEntry)));
                        if (existingMedia != null)
                        {
                            continue;
                        }

                        //Get a suitable MediaData type from extension
                        var mediaType = _contentMediaResolver.GetFirstMatching(Path.GetExtension(fileEntry));
                        var contentType = _contentTypeRepository.Load(mediaType);

                        //Get a new empty file data
                        var media = _contentRepository.GetDefault<MediaData>(uploadFolder.ContentLink,
                            contentType.ID);
                        media.Name = Path.GetFileName(fileEntry);

                        //Create a blob in the binary container
                        var blob = _blobFactory.CreateBlob(media.BinaryDataContainer,
                            Path.GetExtension(fileEntry));
                        using (var fs = new FileStream(fileEntry, FileMode.Open))
                        {
                            blob.Write(fs);
                        }

                        //Assign to file and publish changes
                        media.BinaryData = blob;
                        _contentRepository.Save(media, SaveAction.Publish);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error("Upload image error " + Path.GetFullPath(fileEntry), ex);
                    }
                }
            }
        }

Then from the uploaded media, you can then get its contentLink and create a CommerceMedia like this

var commerceMedia = new CommerceMedia(media.ContentLink, "Image", groupName, order);

and finally add the created commerceMedia to the CommerceMediaCollection of the target product.

I hope this will help.

#203198
Apr 11, 2019 6:28
* 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.