I want to update node properties using code | Community
Skip to main content
Level 6
September 17, 2021
Solved

I want to update node properties using code

  • September 17, 2021
  • 4 replies
  • 1082 views

Node node = resource.adaptTo(Node.class);
final NodeIterator it=node.getNodes();
do {

if(node.getPath().equals(mapNewComponent.getKey()))
{
node.setProperty("slingResourceType","anydata");}
}while(it.hasNext());

 

I am trying this but it is not working

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 BrianKasingli

@ronnie09 ,

I see some problems in your code. I do not see session.save(); and also, I do not an example of how you have obtained thee Node.class. Below is some example code that you can try; however, please keep in mind that you need to create your own service user.

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "myServiceUser");
ResourceResolver resourceResolver = null;
try {
resourceResolver = resourceFactory.getServiceResourceResolver(param);
Resource pageResource = resourceResolver.getResource("/content/my-site/en/home");
Node myPageNode = pageResource.adaptTo(Node.class);
myPageNode.setProperty("nameAs", "brian");
Session session = resourceResolver.adaptTo(Session.class);
session.save();
} catch (Exception exception) {
exception.printStackTrace();
}

 

4 replies

Kishore_Kumar_
Level 9
September 17, 2021
Vijayalakshmi_S
Level 10
September 17, 2021

Hi @ronnie09,

Can you confirm if the control comes inside your "if" condition - "if(node.getPath().equals(mapNewComponent.getKey()))"

If possible, share the complete code and the place where you have this logic.

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
September 17, 2021

@ronnie09 ,

I see some problems in your code. I do not see session.save(); and also, I do not an example of how you have obtained thee Node.class. Below is some example code that you can try; however, please keep in mind that you need to create your own service user.

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "myServiceUser");
ResourceResolver resourceResolver = null;
try {
resourceResolver = resourceFactory.getServiceResourceResolver(param);
Resource pageResource = resourceResolver.getResource("/content/my-site/en/home");
Node myPageNode = pageResource.adaptTo(Node.class);
myPageNode.setProperty("nameAs", "brian");
Session session = resourceResolver.adaptTo(Session.class);
session.save();
} catch (Exception exception) {
exception.printStackTrace();
}

 

Anudeep_Garnepudi
Community Advisor
Community Advisor
September 19, 2021

@ronnie09 

 

Let me understand what you are trying to do.

  • You are getting Node out of a resource, let's call it as resourceNode (in code variable node).
  • You are getting child nodes of resourceNode.

But you are looping and setting the value to the resourceNode always.

Assuming that you are saving the session.

  • If your condition node.getPath().equals(mapNewComponent.getKey()) fails, you don't see the property.
  • If you condition node.getPath().equals(mapNewComponent.getKey()) passes, you will only see the property set resourceNode.

For you current logic, you don't need to loop across all child nodes. You are not at all accessing any child node.

 

Please share what you exactly want to do and the complete code so that we can help.

 

-AG

 

AG