Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

node property handled in js?

Avatar

Level 3

Hi All,

We can remove or add node properties using js ?

if it possible can explain how to do that.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

 

View solution in original post

4 Replies

Avatar

Community Advisor

Dear Vasu,

 

There are many ways to achieve your ask,

 

May we find out more about your use case?


Regards,

Peter

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

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

 

Avatar

Community Advisor

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.