add a new property to a node(page) using ECMA script

余生颓废 提交于 2019-12-11 02:10:23

问题


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.

What am I doing wrong?


回答1:


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();
        }
}

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.

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");


来源:https://stackoverflow.com/questions/18527241/add-a-new-property-to-a-nodepage-using-ecma-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!