You can do it by adding the validation through OnItemCreated
event. Where you can specify your template name (for example Carousel
) and if its child items exceed the limit then you can show the message and delete the item. The code will look like this.
public void OnItemCreated(object sender, EventArgs args)
{
var createdArgs = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;
if (createdArgs != null)
{
if (createdArgs.Item != null)
{
var item = createdArgs.Item;
if (item.Parent != null &&
item.Parent.TemplateName == "carousel" &&
item.Parent.Children.Count() > 8)
{
SheerResponse.Alert(
String.Format("Sorry, you cannot add more than 8 slides to {0}.",
item.Parent.Name), new string[0]);
// write here your logic to delete the item.
}
}
}
}
This works when you create the item from the content editor
and experience editor
.
Once you are done with your method then register it using a patch config like this.
<event name="item:created">
<handler type="MyProject.Sc.Extensions.Events.ItemCreatedEvent, MyProject.Sc.Extensions" method="OnItemCreated" />
</event>
It will look like this.
Hope this helps.