本文介绍了使用ECMA脚本将新属性添加到节点(页面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在页面激活时向页面添加属性。我决定设置一个工作流程,使其在激活步骤之前执行相同的操作。我的自定义工作流程步骤(激活步骤之前的步骤)利用ECMA脚本来实现这一目标。

I need to add a property to a page on page activation. I have decided to set up a workflow process that does the same before an activation step. My custom workflow step (the one before the activation step) makes use of an ECMA script to achieve this. Here's what I have so far.

var workflowData = graniteWorkItem.getWorkflowData();
if (workflowData.getPayloadType() == "JCR_PATH") {
    var path = workflowData.getPayload().toString();
    var jcrsession = graniteWorkflowSession.adaptTo(Packages.javax.jcr.Session);
    var node = jcrsession.getNode(path);
    if (!node.hasProperty("foo")){
    var cal = Packages.java.util.Calendar.getInstance();
            node.setProperty("foo", cal);
            node.save();
         }
if (!node.hasProperty("foo2")){
            node.setProperty("foo2", "2020-08-26T22:30:00.000+05:30");
            node.save();
        }
}

但是,当我在页面上运行工作流程时,

However, when I run the workflow on a page, the properties that I need to get created (foo and foo2 in this instance) do not get created.

我做错了什么?

推荐答案

您是否尝试添加error.log?我尝试了您的脚本,但该脚本无法正常工作-这个特定版本的脚本可以,但是:

have you tried tailing your error.log? i tried your script and it didn't work--this particular version of it does, though:

var workflowData = workItem.getWorkflowData();
if (workflowData.getPayloadType() == "JCR_PATH") {
    var path = workflowData.getPayload().toString();
    var jcrsession = workflowSession.getSession();
    var node = jcrsession.getNode(path);
    if (!node.hasProperty("foo")){
    var cal = Packages.java.util.Calendar.getInstance();
            node.setProperty("foo", cal);
            node.save();
         }
if (!node.hasProperty("foo2")){
            node.setProperty("foo2", "2020-08-26T22:30:00.000+05:30");
            node.save();
        }
}

请注意,它不是花岗石*,而是workItem和workSession。还要注意,WorkflowSession没有一个AdaptTo()方法(除非我使用的cq版本比您旧)。它已经有一个getSession()方法作为接口的一部分。

note that instead of granite*, it's just workItem and workSession. also note that WorkflowSession doesn't have an adaptTo() method (unless i'm using an older cq version than you). it already has a getSession() method as part of the interface.

即使已经说完了,但由于我通过工作流发送的内容而失败了, -确保您要写入的节点接受那些属性名称。 cq:Page的限制非常严格,但cq:PageContent的限制不是(因此,假设您要针对cq:Page或dam:Asset节点启动工作流,请检索jcr:content子节点):

even when that's all said and done, this failed because of the content i was sending through the workflow--make sure the node you're trying to write to accepts those property names. cq:Page is very restrictive, but cq:PageContent is not (so retrieve the jcr:content subnode, assuming you're launching workflows against cq:Page or dam:Asset nodes):

    var node = jcrsession.getNode(path).getNode("jcr:content");

这篇关于使用ECMA脚本将新属性添加到节点(页面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:46