Try our conversational search powered by Generative AI!

"Media not found" - how to delete and find the media relation in later version of commerce


Hi!! Commerce v13.24.0

I have this old code that seems to stopped working. 

We have a Delete Media from Commerce entries functionality. But the problem is that it never finds the asset. How do we find the catalog/product/variant that is related if we only have the media ContentReference?

Old code, which is not working anylonger: 

            if (_contentRepository.TryGet<MediaData>(new ContentReference(mediaId), out MediaData existingMediaData))
                CommerceMedia media = new CommerceMedia(existingMediaData.ContentLink, "episerver.core.icontentmedia", "default", 0);
                CatalogRelationDto relations = CatalogContext.Current.GetCatalogRelationDto(media.AssetLink.ID);

                _logger.Debug($"{relations?.CatalogItemAsset?.Count} CatalogItemAssets found");
                _logger.Debug($"{relations?.CatalogEntryRelation?.Count} CatalogEntryRelations found");
                _logger.Debug($"{relations?.CatalogNodeRelation?.Count} CatalogNodeRelations found");

All Counts are 0. 

Also we used 

foreach (var row in relations.CatalogNodeRelation)

to remove the relation, but i found out that you should do like this with the new apis: 

So, how am i suppose to do to find where an media asset is used? Any ideas?

Edited, Oct 29, 2020 10:42

You can use IContentRepository.GetReferencesToContent, to see if a media is used by any catalog content 

Oct 29, 2020 11:23

Greatness, ill post the final code for future ref! thx Quan, 2 points for you!

Oct 29, 2020 12:15
Dec 30, 2020 10:19
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.