Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

How to Remove CommerceMedia From CommerceMediaCollection

Vote:
0

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?

#201763
Mar 01, 2019 13:40
Vote:
0

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

writable = content.CreateWritableClone() as FashionContent;
writable.CommerceMediaCollection.Remove(...);

#201765
Mar 01, 2019 15:43
Vote:
0

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

#201800
Mar 04, 2019 8:44
Vote:
0

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

#201806
Mar 04, 2019 9:45
Vote:
0

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

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

clone.CommerceMediaCollection.Remove(itemToRemove);

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.

#201813
Mar 04, 2019 10:59
Vote:
0

Any suggestions?

#201881
Mar 06, 2019 8:37
Vote:
1

It just does not make senses, 

clone.CommerceMediaCollection.Remove(itemToRemove);

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?

#201923
Mar 07, 2019 9:59
Vote:
0

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 https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.contains?view=netframework-4.7.2

the underlying implementation of CommerceMediaCollection is List<T>

#201924
Mar 07, 2019 10:02
Vote:
0

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.

#201927
Mar 07, 2019 11:12
Vote:
0

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

#201979
Mar 11, 2019 10:34
Vote:
0

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

#202014
Mar 12, 2019 6:41
Vote:
0

Okej, thanks Quan.

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

#202136
Mar 15, 2019 8:19
Vote:
0

Okej, thanks Quan.

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

#202137
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.