Search code examples
aemjcr

How to create node "var/foo/bar" instead of node.addNode("foo").addNode("baar")?


Is it possible to creeate Node var/foo/baar/ in one step instead of node.addNode("foo").addNode("baar");?

    Resource resource = resourceResolver.getResource("/var");
    Node node = resource.adaptTo(Node.class);
    Node nodeOfTheFile = node.addNode("foo").addNode("baar");
    JcrUtils.putFile(nodeOfTheFile ,filename, "text/csv", inputStream);

How to handle, if the nodes already exists by the creation of the nodes?


Solution

  • You are already using JcrUtils, so you can use one of the createPath methods. They create intermediate node if they not exists and you can even define the node type of them: http://docs.adobe.com/docs/en/cq/current/javadoc/com/day/cq/commons/jcr/JcrUtil.html