Hi All,
We can remove or add node properties using js ?
if it possible can explain how to do that.
Solved! Go to Solution.
Views
Replies
Total Likes
@dh54220 you can actually use JCR/Sling APIs in sling models to manipulations to the node properties.
Resource resource = request.getResourceResolver().getResource("nodePath");
Session session = resource.adaptTo(Session.class);
//Create a node that represents the root node
Node root = session.getRootNode();
// Store content
Node adobe = root.addNode("adobe");
Node aem = adobe.addNode("aem");
day.setProperty("message", "This is sample message");
session.save();
session.logout();
Sling API:
To create node-
Resource myResource = resourceResolver.getResource("/myresource");
Map<String,Object> properties = new HashMap<String,Object>();
properties.put("jcr:primaryType", "nt:unstructured");
properties.put("sling:resourceType", "myapp/components/mytype");
Resource dummy = resourceResolver.create(myResource, "dummy", properties);
resourceResolver.commit();
To remove node-
Resource myResource = resourceResolver.getResource("/myresource");
resourceResolver.delete(myResource);
resourceResolver.commit();
To remove properties-
Resource myResource = resourceResolver.getResource("/myresource");
ModifiableValueMap properties = myResource.adaptTo(ModifiableValueMap.class);
properties.remove("title");
resourceResolver.commit();
Dear Vasu,
There are many ways to achieve your ask,
May we find out more about your use case?
Regards,
Peter
Hi @dh54220
CRUD operation in node can happen via sling(treat as resource)or node api which can only be done via server side.
You can use server side approach with above api.
Regards,
Arpit Varshney
@dh54220 you can actually use JCR/Sling APIs in sling models to manipulations to the node properties.
Resource resource = request.getResourceResolver().getResource("nodePath");
Session session = resource.adaptTo(Session.class);
//Create a node that represents the root node
Node root = session.getRootNode();
// Store content
Node adobe = root.addNode("adobe");
Node aem = adobe.addNode("aem");
day.setProperty("message", "This is sample message");
session.save();
session.logout();
Sling API:
To create node-
Resource myResource = resourceResolver.getResource("/myresource");
Map<String,Object> properties = new HashMap<String,Object>();
properties.put("jcr:primaryType", "nt:unstructured");
properties.put("sling:resourceType", "myapp/components/mytype");
Resource dummy = resourceResolver.create(myResource, "dummy", properties);
resourceResolver.commit();
To remove node-
Resource myResource = resourceResolver.getResource("/myresource");
resourceResolver.delete(myResource);
resourceResolver.commit();
To remove properties-
Resource myResource = resourceResolver.getResource("/myresource");
ModifiableValueMap properties = myResource.adaptTo(ModifiableValueMap.class);
properties.remove("title");
resourceResolver.commit();
Hi @dh54220
Quick question: Why do you need to delete a node via JS? It is always recommended to do it using JAVA (BE Code) where you can manage the session / resourceresolver in an effective manner to avoid memory leaks.
Here are some examples:
https://www.tabnine.com/code/java/methods/javax.jcr.Node/remove
Hope this helps!
Thanks,
Kiran Vedantam.
Views
Like
Replies
Views
Likes
Replies