How to create a series of nodes in AEM | Community
Skip to main content
Mario248
Level 7
January 26, 2023
Solved

How to create a series of nodes in AEM

  • January 26, 2023
  • 2 replies
  • 2070 views

I want to create a series of nodes like parentProduct/subCategory1/subCategory2/subCategory3/subCategory4 inside /var/temp location. I tried to use addNode() method of Node class and createPath() method of JcrUtil class but both did not work. addNode() method just creates only one immediate node(ex, parentProduct) but it is creating second level onwards.

 

Is there any API available that can create a series of nodes, ex-parentProduct/subCategory1/subCategory2/subCategory3/subCategory4?

 

protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException { final Session session; final ResourceResolver resourceResolver = request.getResourceResolver(); session = resourceResolver.adaptTo(Session.class); long count = 0; final String path = "/var/temp"; final PrintWriter out = response.getWriter(); try { if (session.nodeExists(path)) { Node jcrNode = session.getNode(path); jcrNode.addNode("parentProduct/subCategory1/subCategory2/subCategory3/subCategory4"); //jcrNode.addNode("parentProduct"); //JcrUtil.createPath("parentProduct/subCategory1/subCategory2/subCategory3/subCategory4",JcrConstants.NT_UNSTRUCTURED, session); } session.save(); } catch (ItemExistsException ex) { LOG.error("ItemExistsException", ex); } catch (RepositoryException exp) { LOG.error("RepositoryException", exp); } finally { if (session != null) { session.logout(); } } }
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi @mario248,

JcrUtil is way to go, for the method you have used createPath(java.lang.String absolutePath, java.lang.String nodeType, Session session) you need to set absolute path, e.g if you want to create structure under /var code should look like this:

 

JcrUtil.createPath("/var/temp/parentProduct/subCategory1/subCategory2/subCategory3/subCategory4",JcrConstants.NT_UNSTRUCTURED, session); session.save();

 

This works for me. You can also explore other createPath methods, but in general all of them will allow you to achieve your goal.

2 replies

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
January 26, 2023

Hi @mario248,

JcrUtil is way to go, for the method you have used createPath(java.lang.String absolutePath, java.lang.String nodeType, Session session) you need to set absolute path, e.g if you want to create structure under /var code should look like this:

 

JcrUtil.createPath("/var/temp/parentProduct/subCategory1/subCategory2/subCategory3/subCategory4",JcrConstants.NT_UNSTRUCTURED, session); session.save();

 

This works for me. You can also explore other createPath methods, but in general all of them will allow you to achieve your goal.

Kiran_Vedantam
Community Advisor
Community Advisor
January 26, 2023

Hi @mario248 

 

Resource resolver from the request in a servlet will have the access of the logged-in user, if the user is admin, we can get admin access from the request or if the user is not having delete access, performing delete will throw an exception.

 

Please try to create a system user and give appropriate perms to perform any actions in publish environment

 

Hope it helps!

Thanks,
Kiran Vedantam