如何创建不存储响应的 kentico 表单
How to create a kentico form that does not store the response
在 Kentico 中是否有任何方法可以让用户提交表单然后通过电子邮件发送回复但实际上并不将答案保存到相关 table?
是也不是。在发送电子邮件通知和自动回复之前存储记录。最好的办法是使用 BizFormItemEvents.Insert.Before 为表单提交创建一个自定义的全局事件处理程序。这将在实际记录存储在数据库中之前调用该事件。然后您可以取消活动(不会存储记录)并手动发送您的电子邮件。
好吧,有几个不同的选项,但最简单的方法是在插入记录后将其删除。使用全局事件挂钩捕获 BizFormItemEvent 插入之后,如果它是您的表单,则将其删除。以下是 Kentico 10:
using CMS;
using CMS.DataEngine;
using CMS.Forums;
using CMS.Helpers;
using CMS.IO;
using System.Net;
using System.Web;
// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomLoaderModule))]
public class CustomLoaderModule : Module
{
// Module class constructor, the system registers the module under the name "CustomForums"
public CustomLoaderModule()
: base("CustomLoaderModule")
{
}
// Contains initialization code that is executed when the application starts
protected override void OnInit()
{
base.OnInit();
CMS.OnlineForms.BizFormItemEvents.Insert.After += BizFormItem_Insert_After;
}
private void BizFormItem_Insert_After(object sender, CMS.OnlineForms.BizFormItemEventArgs e)
{
switch(e.Item.BizFormInfo.FormName)
{
case "YourFormNameHere":
e.Item.Delete();
break;
}
}
}
另一种选择是克隆和修改联机表单 Web 部件以获取信息,手动调用电子邮件并取消插入,但如果速度更快,工作量会很大。
如前所述,来自 Kentico 的电子邮件依赖于在触发之前写入数据库的记录。此外(除非我运气不好)您唯一可以访问的值是存储在 table 中的值。我曾想也许您可以将有问题的字段标记为 没有数据库表示的字段,但遗憾的是,您可能想要的字段都为空 - 所以最好不要走这条路。
我对@trevor-j-fayas 采取了稍微不同的方法,因为我使用了 BizFormItemEvents.Insert.Before
事件,因此没有任何日志痕迹。距离那里很近,可以使用电子邮件模板使事情看起来不错。所以我的代码如下所示:
using CMS;
using CMS.DataEngine;
using CMS.EmailEngine;
using System;
[assembly: RegisterModule(typeof(FormGlobalEvents))]
public class FormGlobalEvents : Module
{
public FormGlobalEvents() : base("FormGlobalEvents")
{
}
protected override void OnInit()
{
CMS.OnlineForms.BizFormItemEvents.Insert.Before += Insert_Before;
}
private void Insert_Before(object sender, CMS.OnlineForms.BizFormItemEventArgs e)
{
var email = new EmailMessage();
email.From = e.Item.GetStringValue("ContactEmail", "null@foo.com");
email.Recipients = "no-reply@foo.com";
email.Subject = "Test from event handler (before save)";
email.PlainTextBody = "test" + DateTime.Now.ToLongTimeString();
EmailSender.SendEmail(email);
e.Cancel();
}
}
对我来说,首先不插入记录似乎比删除记录更干净,但显然自动回复等只会在您保存记录时自动启动,所以选择权在您,最终取决于根据您的喜好。
在 Kentico 中是否有任何方法可以让用户提交表单然后通过电子邮件发送回复但实际上并不将答案保存到相关 table?
是也不是。在发送电子邮件通知和自动回复之前存储记录。最好的办法是使用 BizFormItemEvents.Insert.Before 为表单提交创建一个自定义的全局事件处理程序。这将在实际记录存储在数据库中之前调用该事件。然后您可以取消活动(不会存储记录)并手动发送您的电子邮件。
好吧,有几个不同的选项,但最简单的方法是在插入记录后将其删除。使用全局事件挂钩捕获 BizFormItemEvent 插入之后,如果它是您的表单,则将其删除。以下是 Kentico 10:
using CMS;
using CMS.DataEngine;
using CMS.Forums;
using CMS.Helpers;
using CMS.IO;
using System.Net;
using System.Web;
// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomLoaderModule))]
public class CustomLoaderModule : Module
{
// Module class constructor, the system registers the module under the name "CustomForums"
public CustomLoaderModule()
: base("CustomLoaderModule")
{
}
// Contains initialization code that is executed when the application starts
protected override void OnInit()
{
base.OnInit();
CMS.OnlineForms.BizFormItemEvents.Insert.After += BizFormItem_Insert_After;
}
private void BizFormItem_Insert_After(object sender, CMS.OnlineForms.BizFormItemEventArgs e)
{
switch(e.Item.BizFormInfo.FormName)
{
case "YourFormNameHere":
e.Item.Delete();
break;
}
}
}
另一种选择是克隆和修改联机表单 Web 部件以获取信息,手动调用电子邮件并取消插入,但如果速度更快,工作量会很大。
如前所述,来自 Kentico 的电子邮件依赖于在触发之前写入数据库的记录。此外(除非我运气不好)您唯一可以访问的值是存储在 table 中的值。我曾想也许您可以将有问题的字段标记为 没有数据库表示的字段,但遗憾的是,您可能想要的字段都为空 - 所以最好不要走这条路。
我对@trevor-j-fayas 采取了稍微不同的方法,因为我使用了 BizFormItemEvents.Insert.Before
事件,因此没有任何日志痕迹。距离那里很近,可以使用电子邮件模板使事情看起来不错。所以我的代码如下所示:
using CMS;
using CMS.DataEngine;
using CMS.EmailEngine;
using System;
[assembly: RegisterModule(typeof(FormGlobalEvents))]
public class FormGlobalEvents : Module
{
public FormGlobalEvents() : base("FormGlobalEvents")
{
}
protected override void OnInit()
{
CMS.OnlineForms.BizFormItemEvents.Insert.Before += Insert_Before;
}
private void Insert_Before(object sender, CMS.OnlineForms.BizFormItemEventArgs e)
{
var email = new EmailMessage();
email.From = e.Item.GetStringValue("ContactEmail", "null@foo.com");
email.Recipients = "no-reply@foo.com";
email.Subject = "Test from event handler (before save)";
email.PlainTextBody = "test" + DateTime.Now.ToLongTimeString();
EmailSender.SendEmail(email);
e.Cancel();
}
}
对我来说,首先不插入记录似乎比删除记录更干净,但显然自动回复等只会在您保存记录时自动启动,所以选择权在您,最终取决于根据您的喜好。