Disable webhooks on #Episerver forms if not finalized
How to override the default actor to disable none finalized forms to be sent.
Published 18th of dec 2019
Episerver Forms 2-5
After a visitor submits a form, Forms.Core calls Actors to perform additional processing on the server.
By default, Episerver Forms has two built-in Actors:
- CallWebhookAfterSubmissionActor
- SendEmailAfterSubmissionActor
A WebHook is a simple event notification via HTTP POST; when a WebHook event is triggered, a HTTP POST request is sent to a URL.
Episerver Forms has built-in WebHooks with the CallWebhookAfterSubmissionActor. As a publisher of the event, WebHooks pushes all form data to the remote URL.
Problem
By default, the Form Step Action Element sends away a webhook action for every step.
Solution
Check the IsFinalized flag by overriding the CallWebhookAfterSubmissionActor. Code below only allows webhooks when forms are finalized.
using System.Collections.Generic; using System.Linq; using System.Web; using EPiServer.Forms.Core.Models; using EPiServer.Forms.Core.PostSubmissionActor; using EPiServer.Forms.Implementation.Actors; using EPiServer.Forms.Implementation.Elements; namespace Gosso.Mvc.Business { public class CustomActorsExecutingService : EPiServer.Forms.Core.PostSubmissionActor.ActorsExecutingService { public override IEnumerable<IPostSubmissionActor> GetFormSubmissionActors(Submission submission, FormContainerBlock formContainer, FormIdentity formIden, HttpRequestBase request, HttpResponseBase response, bool isFormFinalizedSubmission) { var actors = base.GetFormSubmissionActors(submission, formContainer, formIden, request, response, isFormFinalizedSubmission); if (isFormFinalizedSubmission) return actors; //return all return actors.Where(a => !(a is CallWebhookAfterSubmissionActor));//do not return the webhook actor } } }
And … register the custom actor
private static void ConfigureContainer(StructureMap.ConfigurationExpression container) { container.For<ActorsExecutingService>().Use<CustomActorsExecutingService>(); }
Read more:
- https://world.episerver.com/documentation/developer-guides/forms/implementing-a-customized-actor/
- https://github.com/episerver/EPiServer.Forms.Demo
- https://www.dcaric.com/blog/episerver-forms-3-custom-actors
- https://getadigital.com/blog/replacing-built-in-email-actor-with-custom-implementation-in-episerver-forms/
- https://www.vegaitsourcing.rs/media-center/blog/how-to-send-an-email-with-attachments-using-episerver-forms
About the author
Luc Gosso
– Independent Senior Web Developer
working with Azure and Episerver
Twitter: @LucGosso
LinkedIn: linkedin.com/in/luc-gosso/
Github: github.com/lucgosso