Java JCR/Adobe AEM: How do I access the node directly without using queries? | Community
Skip to main content
jayv25585659
Level 8
Aug 16, 2021 11:51 AM UTC
Solved

Java JCR/Adobe AEM: How do I access the node directly without using queries?

as above. I've looked at "getNode" (part of javax.jcr.node) and it requires a relative path. I'm hoping there's a function that I haven't seen yet that accepts an absulate path.

 

Code example:

 

Node node = Node.getNodeByAbsulotePath("/content/my-folder-here/my-node-here");

 

Thanks a lot.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
vmadala
vmadalaAccepted solution
Level 3
Aug 16, 2021 12:13 PM UTC

Hello @jayv25585659 ,

 

You can use 

@Inject
@Source("sling-object")
private ResourceResolver resourceResolver;
resourceResolver.getResource(<path>);

 

If this code is in Model class (Basically in Author) you don't need any service user, But if you want to get ResourceResolver in any other services like servlets you need service user with permission to access that node.

And if you want create if node doesn't exists, can use below method

 

ResourceUtil.getOrCreateResource(resolver, path, ....);

 

 

4 replies

arunpatidar
New Participant
Aug 16, 2021 1:17 PM UTC
jayv25585659
Level 8
Aug 16, 2021 3:47 PM UTC

This is the one I was looking for. I know I've used it before (workflow code) but I cannot remember where to look. Thank you.

Translate
vmadala
vmadalaAccepted solution
Level 3
Aug 16, 2021 12:13 PM UTC

Hello @jayv25585659 ,

 

You can use 

@Inject
@Source("sling-object")
private ResourceResolver resourceResolver;
resourceResolver.getResource(<path>);

 

If this code is in Model class (Basically in Author) you don't need any service user, But if you want to get ResourceResolver in any other services like servlets you need service user with permission to access that node.

And if you want create if node doesn't exists, can use below method

 

ResourceUtil.getOrCreateResource(resolver, path, ....);

 

 

Translate
jayv25585659
Level 8
Aug 16, 2021 12:17 PM UTC

Forgot about that! It's been awhile since I've used it. Let me try that tomorrow. Thank you.

 

Edit: I'm extending wcmpojo (or whatever it is called) in my class.

Translate