Rename filename in CommerceMedia Collection

Vote:
 

Hi,

I am looking into the possibility of renaming some images that have been assigned to a product in episerver commerce.

As I see it the only way that I can achieve would I would like to do is.

  1. Get the original image.
  2. Duplicate the original image by creating a new instance and setting all the properties.
  3. Set the filename.
  4. Save the image.
  5. Associate the image to the product.
  6. Remove / delete the original image.

Seems alot of work to rename a filename.

Has anyone tried anything similar?

Thanks

Paul

#241014
Dec 09, 2020 21:53
Vote:
 

The image is associated with the product by its content guid, which is permanent. What you can do is probably this (pseudo untested code)

var contentLink = product.CommerceMediaCollection.First().AssetLink;

var media = contentRepository.Get<MediaData>(contentLink);

media.Name = "Can I actually do this";

contentRepository.Save(media, SaveAction.Publish, AccessLevel.None);

#241036
Dec 10, 2020 8:26
Vote:
 

This is pretty close. You'll also want to update the RouteSegment value to show the updated name in the image url, and before you do anything create a writeable clone as the object returned by the repository will be read only.  So something like;

var contentLink = product.CommerceMediaCollection.First().AssetLink;

var media = contentRepository.Get<MediaData>(contentLink);

var mediaClone = (ImageData)media.CreateWritableClone()

mediaClone.Name = "Can I actually do this";

mediaClone.RouteSegment = "Can I also do this"

contentRepository.Save(mediaClone, SaveAction.Publish, AccessLevel.None);

#245475
Dec 11, 2020 15:38
Vote:
 

Thanks Quan and David

#245476
Dec 11, 2020 16:07
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.