Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Creating sub-node within jcr:content - 3rd node from jcr:content is not created

Avatar

Level 4

Hi all,

 
We are using code to programitacally create node in CRX. Using AEM 6.1 . Found that if we want to create 3rd node from "jcr:content" then it does not create 3rd node (in this case 3rd node is "n3").
 
Refer to this adobe link
https://helpx.adobe.com/experience-manager/using/programmatically-accessing-cq-content-using.html
 
 
 
Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server");
   
   //Create a Session
   javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray()));
 
  //Create a node that represents the root node
  Node root = session.getRootNode();
 
  // Create Node
Node n2 = root.getNode("content/mysite/mypage/jcr:content/n1/n2")
  Node n3 = root.addNode("n3");
 
NOTE : Node "n2" already exists. (content/mysite/mypage/jcr:content/n1/n2)
 
Any reason why the above code does not create node at 3rd level from "jcr:content" ? Is there any limitation of JCR ? 
 
Using same code if we use "addNode" for creating any node parallel to "n1" then we are able to create that node
 
   // Create Node
Node n1 = root.getNode("content/mysite/mypage/jcr:content/n1")
  Node n11 = root.addNode("n11");
1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

If you see the documentation you may find that "addNode()" function accepts the relative path. Its is not aware of any level structure.

Now lets say you call  root.addNode("x1"); in this case root path is mypage/jcr:content so mypage/jcr:content/x1 gets created.

From here if you want to creates node under mypage/jcr:content/x1 you need to change the reference from which you calling.

Node n = session.getNode("mypage/jcr:content/x1").

Node x2 = n.addNode("x2");

Now this will create a node similar to  mypage/jcr:content/x1/x2.

Keep in mind that its relative path to the node from which you are calling it.

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

Hi,

If you see the documentation you may find that "addNode()" function accepts the relative path. Its is not aware of any level structure.

Now lets say you call  root.addNode("x1"); in this case root path is mypage/jcr:content so mypage/jcr:content/x1 gets created.

From here if you want to creates node under mypage/jcr:content/x1 you need to change the reference from which you calling.

Node n = session.getNode("mypage/jcr:content/x1").

Node x2 = n.addNode("x2");

Now this will create a node similar to  mypage/jcr:content/x1/x2.

Keep in mind that its relative path to the node from which you are calling it.