Kentico 如何防止由API调用引起的全局事件处理程序触发
我有一个自定义模块,它使用Kentico API(DocumentHelper)更新文档的某些字段,然后发布,但我不希望它触发链接到文档页面类型的事件处理程序。我尝试向.Publish(“admin_edit”)添加注释,希望可以从WorkflowEventargs参数中捕获注释,但VersionComment属性始终返回null。肯蒂科有没有办法做到这一点 更新字段:Kentico 如何防止由API调用引起的全局事件处理程序触发,kentico,Kentico,我有一个自定义模块,它使用Kentico API(DocumentHelper)更新文档的某些字段,然后发布,但我不希望它触发链接到文档页面类型的事件处理程序。我尝试向.Publish(“admin_edit”)添加注释,希望可以从WorkflowEventargs参数中捕获注释,但VersionComment属性始终返回null。肯蒂科有没有办法做到这一点 更新字段: var document = DocumentHelper.GetDocument(documentID, tree)
var document = DocumentHelper.GetDocument(documentID, tree);
var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(document);
if (workflow != null)
{
document.CheckOut();
document.SetValue("SomeFIeld", "some value");
document.Update(true);
document.CheckIn();
document.Publish("admin_edit");
}
事件处理程序:
public override void Init()
{
WorkflowEvents.Publish.After += Publish_After;
}
private void Publish_After(object sender, WorkflowEventArgs e)
{
if (!string.IsNullOrEmpty(e.VersionComment) &&
e.VersionComment.Contains("admin_edit"))
return;
}
版本信息总是为null,因为这与“页面版本控制”事件有关,特别是“保存版本”。你可以在这上面找到更多。如果展开“属性”,您将看到为特定事件填充了哪些属性。在您的情况下,您可以尝试以下操作,为最新版本添加消息,然后在“发布后”事件中检查该注释,请参见下面的代码:
var document = DocumentHelper.GetDocument(documentID, tree);
var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(document);
if (workflow != null)
{
document.CheckOut();
document.SetValue("SomeFIeld", "some value");
document.Update(true);
document.CheckIn(versionComment: "admin_edit");
document.Publish();
}
然后,在事件处理程序中,获取最新版本并检查注释,如下所示:
if (e.PublishedDocument?.VersionHistory?.Count > 0)
{
var lastVersion = e.PublishedDocument.VersionHistory[0] as VersionHistoryInfo;
if (lastVersion.VersionComment.Equals("admin_edit"))
{
return;
}
}
注意:如果您有很多并发内容编辑器,则您的上一个版本可能不是API版本(有人更改了内容,并在API调用更改后立即保存了内容)。这种可能性很小,但仍然是可能的。如果这是你经常使用的东西,你必须考虑它。这段代码是针对Kentico 11进行测试的谢谢,我会尝试一下。