Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How to access JCR nodes using code

Ronnie09
Level 4
Level 4

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
fanindras
Correct answer by
Level 5
Level 5

@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
fanindras
Correct answer by
Level 5
Level 5

@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

Jörg_Hoh
Employee
Employee

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