I have a MainSidebar Document Type and I've written code in the publishing event to prevent more than one of these being created, code below.
This works, and the publishing is prevented, but a faded content document still exists in the content area that needs to be deleted. How can I also delete, or discard, the content please?
[There is ent.ChangeTrashedState possibly(?).]
public class Subscription : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
Umbraco.Core.Services.ContentService.Publishing += ContentService_Publishing;
}
private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
{
foreach (var ent in e.PublishedEntities)
{
if (ent.ContentType.Alias.ToUpper() == "MAINSIDEBAR")
{
e.CancelOperation(new Umbraco.Core.Events.EventMessage("Sidebar", "Only one sidebar allowed", Umbraco.Core.Events.EventMessageType.Error));
LogHelper.Error(typeof(Subscription), $"Attempt to create additional sidebar(s) (writer id: {ent.WriterId})", new ApplicationException());
}
}
}
}
I tried using Saving, and it kind-of worked, but generated an additional (404) error and left the backoffice in an indeterminate state (just with a loading icon).
I've reverted to Publishing (with a Warn log) for the moment, but it's not as good as I'd like it :(
private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
{
foreach (var ent in e.PublishedEntities)
{
if (ent.ContentType.Alias.ToUpper() == "MAINSIDEBAR")
{
e.CancelOperation(new Umbraco.Core.Events.EventMessage("Sidebar", "Only one sidebar allowed", Umbraco.Core.Events.EventMessageType.Error));
LogHelper.Warn(typeof(Subscription), $"Attempt to create additional sidebar(s) (writer id: {ent.WriterId})");
}
}
}
publishing event, delete the content
I have a MainSidebar Document Type and I've written code in the publishing event to prevent more than one of these being created, code below.
This works, and the publishing is prevented, but a faded content document still exists in the content area that needs to be deleted. How can I also delete, or discard, the content please?
[There is ent.ChangeTrashedState possibly(?).]
Darn, it just occurred to me that I should use the Creating event, doi! That should hopefully solve.
I now have a similar issue with the Creating version, in that it still goes into the document properties, and it no longer shows my notification.
Looking for some hints ;)
The way I've done it in the past has been something like:
Not sure if that works better?
If you want to log, I wouldn't raise an exception - use a warn instead.
Thank you.
This runs, and it executes e.Cancel = true; but nothing happens in the UI/backoffice: the content is saved and appears in the tree.
I also tried using Created, as a notice in VS says that Creating is obsolete, but, again, nothing happens.
I tried using Saving, and it kind-of worked, but generated an additional (404) error and left the backoffice in an indeterminate state (just with a loading icon).
I've reverted to Publishing (with a Warn log) for the moment, but it's not as good as I'd like it :(
is working on a reply...