Try our conversational search powered by Generative AI!

How to avoid duplicates of contents in a ContentArea


Is there an easy way to avoid duplicates in the ContentArea?

Have searched but not found much.... I prefer not to hook up in publish event att do the check myself..

Attribute like [DisableDuplicates] would have been perfect here

Apr 18, 2016 10:49

How about creating a custom validator for this? You could either give a warning or an error message to the editors.

Apr 18, 2016 11:01

Yes, I tried it and it works perfectly!!

Thanks a lot, this made my day :)

A minor thing... would be great to be able to present the actual proprerty display name to the editor (and not the actual property name)...

Apr 18, 2016 11:27

Regarding the display name, you could check out Grzegorz answer in this forum thread:

Apr 18, 2016 11:48

Yes I found his answer and borrowed it :-)

Here is the code if anyone is interested, not many rows

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
	public sealed class DisableDuplicates : ValidationAttribute
		protected override ValidationResult IsValid(object value, ValidationContext validationContext)
			var propertyDefinitionRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IPropertyDefinitionRepository>();
			var propertyDefinitionId = ((PageData)validationContext.ObjectInstance).Property[validationContext.MemberName].PropertyDefinitionID;
			var propertyDefinition = propertyDefinitionRepository.Load(propertyDefinitionId);
			propertyDefinition.LocalizationService = LocalizationService.Current;

			if (IsDuplicate(value as ContentArea))
				return new ValidationResult(
					$"You can not have duplicate content in: '{propertyDefinition.TranslateDisplayName()}'",
					new[] { validationContext.MemberName });

			return ValidationResult.Success;

		bool IsDuplicate(ContentArea area)
			if (area == null || area.IsEmpty)
				return false;

			if (area.Items == null || area.Items.Any() == false)
				return false;

			List<int> items = new List<int>();

			foreach (var cai in area.Items)
				if (items.Contains(cai.ContentLink.ID))
					return true;


			return false;
Apr 18, 2016 11:49

Thanks for sharing! :-)

Apr 18, 2016 11:53

Sounds like a good solution. I might steal it :)

Apr 18, 2016 12:10

You could also simplify IsDuplicate method to single line expression :)

private bool IsDuplicate(ContentArea area)
    return area?.Items.DistinctBy(c => c.ContentLink.ToReferenceWithoutVersion()).Count() != area?.Items.Count;
Apr 18, 2016 13:24


Apr 18, 2016 13:44
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.