Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Cannot remove duplicate item from ContentArea

Found in

EPiServer.CMS.Core 7.5.394.2

Fixed in

EPiServer.CMS.Core 11.2.0

(Or a related package)

Created

Oct 27, 2017

Updated

Nov 27, 2017

Area

CMS Core

State

Closed, Fixed and tested


Description

Steps to reproduce

  1. Add duplicated blocks (multiple times) into a ContentArea of a page.
  2. Trying to iterate through the items and remove them programmatically.

        private void RemoveAllContentAreaItems(ContentArea contentArea)
        {
            int contentAreaItemsCount = contentArea.Count;
            for (int i = 0; i < contentAreaItemsCount; i++)
            {
                try
                {
                    contentArea.Items.RemoveAt(0);
                }
                catch (Exception e)
                {
                    Logger.Error("Could not remove block from Content Area", e);
                }
            }
            contentArea.IsModified = true;
        }

Expected: Can delete items from a content area programmatically.
Actual: Get the following exception:

System.InvalidOperationException: Sequence contains more than one matching element
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at EPiServer.Core.ContentArea.RemoveContentFragment(ContentReference contentlink)
   at EPiServer.Core.ContentArea.ModifyCollectionInternally(Action modification)
   at EPiServer.Core.ContentArea.OnContentAreaItemsChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)