You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Umbraco Forms后台添加删除事件?缺失删除工作流选项

How to Add Custom Logic for Form Entry Deletion in Umbraco Forms

Got it, let's break down how to implement custom deletion tracking since Umbraco Forms doesn't include a built-in delete workflow option. We'll use Umbraco's event/notification system to hook into the entry deletion action and run your custom logging or record-keeping code.

For Umbraco 9+ (.NET Core)

Umbraco switched to a notification-based system in v9, so we'll create a notification handler to listen for when a form entry is deleted.

Step 1: Create the Notification Handler

This class will handle the FormEntryDeletedNotification and execute your custom logic when the event fires.

using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Forms.Core.Models;
using Umbraco.Forms.Core.Notifications;
using Microsoft.Extensions.Logging;

public class FormEntryDeletedHandler : INotificationHandler<FormEntryDeletedNotification>
{
    private readonly ILogger<FormEntryDeletedHandler> _logger;
    // Inject any other services you need (e.g., a custom repository for deletion logs)

    public FormEntryDeletedHandler(ILogger<FormEntryDeletedHandler> logger)
    {
        _logger = logger;
    }

    public void Handle(FormEntryDeletedNotification notification)
    {
        // Get the deleted entry details
        FormEntry deletedEntry = notification.Entry;

        try
        {
            // Example: Log the deletion to Umbraco's log
            _logger.LogInformation(
                "Form Entry Deleted: ID {EntryId}, Form ID {FormId}, Deleted At {DeleteTime}",
                deletedEntry.Id,
                deletedEntry.FormId,
                DateTime.UtcNow);

            // Add your custom logic here:
            // - Write to a custom database table (include user info if needed)
            // - Trigger an external webhook
            // - Update related content nodes
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to process deletion for form entry {EntryId}", deletedEntry.Id);
            // Don't rethrow unless you want to block the deletion; log and continue
        }
    }
}

Step 2: Register the Handler with Umbraco's DI

Create a composer to register your handler so Umbraco knows about it:

using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Forms.Core.Notifications;

public class FormEntryDeletedComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.AddNotificationHandler<FormEntryDeletedNotification, FormEntryDeletedHandler>();
    }
}

For Umbraco 7/8 (.NET Framework)

Older versions use an event-based system instead of notifications. Here's how to hook into the entry deletion event:

Step 1: Create a Component

Components run when Umbraco starts, so we'll use one to subscribe to the deletion event.

using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Forms.Core.Services;
using Umbraco.Core.Logging;

[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class FormEntryDeletedComponent : IComponent
{
    private readonly IEntryService _entryService;
    private readonly ILogger _logger;

    public FormEntryDeletedComponent(IEntryService entryService, ILogger logger)
    {
        _entryService = entryService;
        _logger = logger;
    }

    public void Initialize()
    {
        // Subscribe to the entry deleted event
        _entryService.Deleted += OnEntryDeleted;
    }

    private void OnEntryDeleted(object sender, Umbraco.Forms.Core.Events.EntryEventArgs e)
    {
        FormEntry deletedEntry = e.Entry;

        try
        {
            // Log the deletion
            _logger.Info(
                typeof(FormEntryDeletedComponent),
                $"Form Entry {deletedEntry.Id} (Form {deletedEntry.FormId}) deleted at {DateTime.Now}");

            // Add your custom record-keeping logic here
        }
        catch (Exception ex)
        {
            _logger.Error(typeof(FormEntryDeletedComponent), ex, $"Error processing deletion for entry {deletedEntry.Id}");
        }
    }

    public void Terminate()
    {
        // Unsubscribe to prevent memory leaks
        _entryService.Deleted -= OnEntryDeleted;
    }
}

Step 2: Register the Component

Create a composer to register your component:

using Umbraco.Core.Composing;

public class FormEntryDeletedComposer : ComponentComposer<FormEntryDeletedComponent>
{
}

Key Notes

  • User Tracking: If you need to record who deleted the entry, inject IHttpContextAccessor (v9+) or UmbracoContext (v7/8) to get the current logged-in user's details.
  • NuGet Packages: Ensure you have the correct Umbraco Forms NuGet package installed (e.g., Umbraco.Forms.Core for v9+).
  • Testing: After deploying your code, delete a test form entry and verify your logs/records are created as expected.

Let me know if you need help adapting this to your specific Umbraco version or expanding the logic!

内容的提问来源于stack exchange,提问作者Rajinder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:14:03