Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to access JCR nodes using code

Avatar

Level 7

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

@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

Avatar

Employee Advisor

@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)?