Expand my Community achievements bar.

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

How to create a series of nodes in AEM

Avatar

Level 9

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

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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.

Avatar

Community Advisor

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