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
Solved! Go to Solution.
Views
Replies
Total Likes
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();
}
Save the session at last using session.save() method.
https://www.albinsblog.com/2014/08/programmatically-creating-nodes-in.html#.YUSsJ7gzbIU
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.
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();
}
Let me understand what you are trying to do.
But you are looping and setting the value to the resourceNode always.
Assuming that you are saving the session.
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
Views
Likes
Replies
Views
Like
Replies