Expand my Community achievements bar.

SOLVED

I want to update node properties using code

Avatar

Level 7

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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();
}

 

View solution in original post

4 Replies

Avatar

Community Advisor

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.

Avatar

Correct answer by
Community Advisor

@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();
}

 

Avatar

Community Advisor

@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