How to access JCR nodes using code | Community
Skip to main content
Level 6
September 16, 2021
Solved

How to access JCR nodes using code

  • September 16, 2021
  • 2 replies
  • 2793 views

I am trying to retrieve the data from node and update as well.

This is the code that I am trying to run

try { //Create a connection to the CQ repository running on local host Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/de/index.jsp#/content/launches/2021/09/16/Tool"); //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(); // Retrieve content Node node = root.getNode("jcr:content/sources/source_0"); LOG.info(node.getProperty("sourceRootResource").getString()+"This is sourceRootPath"); node.setProperty("sourceRootResource", "/content//websites/en_us/pyramid"); LOG.info("Property SET"); // Save the session changes and log out session.save(); session.logout(); } catch(Exception e){ LOG.error("Cant update node", e); }

 

This is popping error.
The following RepositoryFactory classes were consulted:
Perhaps the repository you are trying to access is not available at the moment.

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 Fanindra_Surat

@ronnie09 - Use Service Resource resolver to get access to the AEM internal resources, and then update the node properties. 

 

You can create a system user with appropriate permissions to the /content/launches/* path and use this to configure service for fetching the service resource resolver.

Some useful links for fetching service resource resolver - 

https://aem4beginner.blogspot.com/how-to-get-resourceresolver-from

http://www.aemcq5tutorials.com/tutorials/resourceresolver-from-resourceresolverfactory/

 

Once you have the resource resolver object, you can refer to the following sample code:

 

// Fetch the node

Resource resource = resourceResolver.getResource("/content/launches/2021/09/16/Tool/jcr:content/sources/source_0");

Node node = resource.adaptTo(Node.class);

 

// Update the node

node.setProperty("sourceRootResource", "/content//websites/en_us/pyramid");

 

// Save the session

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

session.save();

 

Thanks,

Fani

2 replies

Fanindra_Surat
Community Advisor
Fanindra_SuratCommunity AdvisorAccepted solution
Community Advisor
September 16, 2021

@ronnie09 - Use Service Resource resolver to get access to the AEM internal resources, and then update the node properties. 

 

You can create a system user with appropriate permissions to the /content/launches/* path and use this to configure service for fetching the service resource resolver.

Some useful links for fetching service resource resolver - 

https://aem4beginner.blogspot.com/how-to-get-resourceresolver-from

http://www.aemcq5tutorials.com/tutorials/resourceresolver-from-resourceresolverfactory/

 

Once you have the resource resolver object, you can refer to the following sample code:

 

// Fetch the node

Resource resource = resourceResolver.getResource("/content/launches/2021/09/16/Tool/jcr:content/sources/source_0");

Node node = resource.adaptTo(Node.class);

 

// Update the node

node.setProperty("sourceRootResource", "/content//websites/en_us/pyramid");

 

// Save the session

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

session.save();

 

Thanks,

Fani

joerghoh
Adobe Employee
Adobe Employee
September 17, 2021

@ronnie09 Are you trying to run the code from within AEM or are you trying to access the JCR repository from a different JVM (not AEM)?