I want to update node properties using code | Community
Skip to main content
Level 6
Sep 17, 2021 2:41 PM UTC
Solved

I want to update node properties using code

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.
BrianKasingli
BrianKasingliAccepted solution
New Participant
Sep 17, 2021 9:47 PM UTC

@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

Anudeep_Garnepudi
New Participant
Sep 19, 2021 9:03 AM UTC

@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

 

Translate
BrianKasingli
BrianKasingliAccepted solution
New Participant
Sep 17, 2021 9:47 PM UTC

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

 

Translate
Vijayalakshmi_S
Level 10
Sep 17, 2021 5:45 PM UTC

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.

Translate
Kishore_Kumar_
Level 9
Sep 17, 2021 2:55 PM UTC

Save the session at last using session.save() method.

 

https://www.albinsblog.com/2014/08/programmatically-creating-nodes-in.html#.YUSsJ7gzbIU 

Translate