Try our conversational search powered by Generative AI!

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

Vote:
 

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)
{
    row.Delete();
}

to remove the relation, but i found out that you should do like this with the new apis:  https://support.episerver.com/hc/en-us/articles/115003738211-Deleting-CommerceMedia-leaves-the-asset-row-intact 

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

#230073
Edited, Oct 29, 2020 10:42
Vote:
 

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

#230075
Oct 29, 2020 11:23
Vote:
 

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

#230076
Oct 29, 2020 12:15
Vote:
 
#246204
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.