November Happy Hour will be moved to Thursday December 5th.

Assets aren't removed/unlinked when an image is deleted?

Vote:
 

When we import data from the PIM system we use the content API. When an image is deleted in PIM we use IContentRepository.Delete(), but this doesn't seem to unlink that image from any commerce content that has that image. Is this expected? Should we be calling some commerce API when an image is removed to ensure that it's removed from the assets as well?

What happens for us now is:

1) Product is born in PIM and sent to Episerver

2) Images are added to it in PIM and sent to Episerver and we update CommerceMediaCollection on the product and save

3) The image is deleted in PIM which is sent to Episerver, and we call IContentRepository.Delete() for that image

4) The next time we try to save the product we get a ValidationException saying "Media is not found. Navigate to Assets tab and remove it in order to publish."

Versions:

Commerce: 11.4.1

CMS: 10.10.4

#188589
Edited, Feb 27, 2018 13:35
Vote:
 

No, it's not deleted by default, especially if you are using APIs to do it (no warning etc.). You would have to find the contents using it and remove the link yourself.

#188590
Feb 27, 2018 13:52
Vote:
 

Ok. What's the reasoning behind keeping the broken link?

There's (to my knowledge) no APIs to find commerce entities which references an image, so I guess I have to query the database directly and then load those content items and remove the asset from CommerceMediaCollection and save. Would be nice if that was included in Episerver. I know that this has been an issue for quite some time. Could this be added as a feature request perhaps? smile

#188591
Feb 27, 2018 13:59
Vote:
 

I have good news for you - Media is just IContent, and you can use ContentProvider.GetReferencesToContentItems to find which catalog contents are using a specific media. 

#188595
Feb 27, 2018 14:34
Vote:
 

You learn something every day, thanks!

#188596
Feb 27, 2018 14:52
Vote:
 

It does not do that automatically, no.

But you could do it yourself as part of the image deletion process by doing something like this:

var referencesToImage = ContentRepository.GetReferencesToContent(imageToRemove.ContentLink, true);
var contentLinks = referencesToImage.Select(x => x.OwnerID);
foreach (var link in contentLinks)
{
	var associatedProduct = ContentRepository.Get<ProductContent>(link);
	var assetToRemove = associatedProduct.CommerceMediaCollection.FirstOrDefault(x => x.AssetLink == imageToRemove.ContentLink);

	if (assetToRemove == null)
		continue; 

	var clone = product.CreateWritableClone<ProductContent>();
	clone.CommerceMediaCollection.Remove(assetToRemove);
	ContentRepository.Save(clone);
}

I haven't tested that code properly so some more error handling may be needed, for example if it's not "ProductContent" that you have your image attached to, but you get the general idea at least. :)

Anyway, after deleting the assets based on the references you can safely Delete() the the "imageToRemove".

If you for some reason already have a ton of products that have this faulty state you could ensure that all CommerceMedia points to something as a part of your saving process, but this approach would be more difficult to manage.

var unlinkedAssets = product.CommerceMediaCollection.Where(x => ContentReference.IsNullOrEmpty(x.AssetLink));
var clone = product.CreateWritableClone<ProductContent>();
foreach (var asset in unlinkedAssets)
{
	clone.CommerceMediaCollection.Remove(asset);
}
ContentRepository.Save(clone);


Edit:

Huh... weird. I didn't see any answers in this thread when I wrote this. But it seems like this has been answered hours before I even stumbled upon the thread. tongue-out

#188614
Edited, Feb 27, 2018 23:47
Vote:
 

It was a bug on world, it has been reported and fixed now.

#188647
Feb 28, 2018 13:57
Vote:
 

Thanks Erik, we've just deployed new version with latest CMS so it might cause several problems. Sorry for any inconvenience that might occur.

#188675
Edited, Mar 01, 2018 1:20
Vote:
 

Ok, that explains it.

#188689
Mar 01, 2018 9:26
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.