Try our conversational search powered by Generative AI!

How to Remove CommerceMedia From CommerceMediaCollection


var commerceMediaToRemove=
writableCommerceMedia.FirstOrDefault(x => x.AssetLink.Equals(contentLink));

writableCommerceMedia.Remove(commerceMediaToRemove); // this returns false

Does anyone have any idea of what im doing wrong?

Mar 01, 2019 13:40

It looks like you clone the CommerceMediaCollection, which is not correct. You should clone the content itself: 

writable = content.CreateWritableClone() as FashionContent;

Mar 01, 2019 15:43

I tried that also, with the same result. It won't allow me to remove the media.

Mar 04, 2019 8:44

Can you post the full snippet here? Did you make sure commerceMediaToRemove is not null? 

Mar 04, 2019 9:45

var clone = currentContent.CreateWritableClone<EntryContentBase>();

var itemToRemove = clone.CommerceMediaCollection.FirstOrDefault(x => x.AssetLink.Equals(contentLink));


contentRepo.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);

We had a product in the CommerceMediaCollection, i'm not sure how it got there. 
After i deleted the product from the db table CatalogItemAsset, the code run successfully. The item was found and .Remove() returned true, however i would want to be able to get it right without going in to the database and manually delete the row. 

Yes, im sure the commerceMediaToRemove was not null.

Mar 04, 2019 10:59

Any suggestions?

Mar 06, 2019 8:37

It just does not make senses, 


is just some standard collection modification, it returns false if the collection does not contain itemToRemove. Can you debug to be sure: which are in the collection, what is in the itemToRemove?

Mar 07, 2019 9:59

The only reasonable explaination I can come up with is itemToRemove is null, and .Remove will return false because it relies on .Contains to check

the underlying implementation of CommerceMediaCollection is List<T>

Mar 07, 2019 10:02

I did debugg the code several times and the itemToRemove was not null.
The CommerceMediaCollection contains 7 items including the itemToRemove item which was at index 0.

Mar 07, 2019 11:12

Then I'd suggest you to contact our developer support service for further assistance

Mar 11, 2019 10:34

What if you clear the collection and re-add items you want to keep?

Mar 12, 2019 6:41

Okej, thanks Quan.

Maris, never thought of that. I will give it a try.

Mar 15, 2019 8:19

Okej, thanks Quan.

Maris, never thought of that. I will give it a try.

Mar 15, 2019 8:19
* 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.