Expand my Community achievements bar.

SOLVED

AEM Add File: Exception occurred: This tree does not exist

Avatar

Level 3

I am trying to create a txt file under /conf but got this error:

Exception occurred: This tree does not exist

 

Here is the simplified version of my code - ignoring the file content:

 

Session session = resourceResolver.adaptTo(Session.class);

Node confNode = session.getNode("/conf");

Node fileNode = confNode.addNode("helloworld.txt", "nt:file");

session.save();

 

Am I missing anything? /conf path exists in CRXDE so I am not sure why it says the tree does not exist. I have also make sure the SystemUser.yaml has the write and read permission under /conf

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

hi @aemUser2345 ,

 

There is no "/conf" node in the repository, so you cannot add a child node to it.

 

trying with this condition. then you can proceed with your code.

 

if (!session.nodeExists("/conf")) {
    Node confRoot = session.getRootNode().addNode("conf");
    session.save();
}

 

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

hi @aemUser2345 ,

 

There is no "/conf" node in the repository, so you cannot add a child node to it.

 

trying with this condition. then you can proceed with your code.

 

if (!session.nodeExists("/conf")) {
    Node confRoot = session.getRootNode().addNode("conf");
    session.save();
}

 

Avatar

Community Advisor

Hi @aemUser2345 

Make sure you create jcr:content node as well

 

javax.jcr.nodetype.ConstraintViolationException: OakConstraint0025: /conf/helloworld.txt[[nt:file]]: Mandatory child node jcr:content not found in a new node


Arun Patidar

Avatar

Community Advisor

Hi @aemUser2345 ,

 You need to add jcr:content also as suggest by @arunpatidar .

Update the code as below

Session session = resourceResolver.adaptTo(Session.class);
Node confNode = session.getNode("/conf");
Node fileNode = confNode.addNode("helloworld.txt", "nt:file");
Node jcrContent = fileNode.addNode("jcr:content", "nt:unstructured");
session.save();

Regards

Shiv

 

Shiv Prakash