Search code examples
aemsling

Replace node along with subnodes in a single request


I have a widget that creates a POST request that creates a node and a dynamic number of subnodes, like:

./sling:resourceType:app/component
_charset_:utf-8
:status:browser
./data:data
./a/a:one
./a/b:two
./b/a:one
./b/b:two

This works nice the first time. I get a node along with subnodes a and b. The problem is in subsequent requests. I need all subnodes to be removed before creating the new ones. So if previously I created subnodes a,b,c and d, the previous request would result just in subnodes a and b to remain.

I know the suffix @Delete,but I would need to know in advance which subnodes need to be deleted, which I don't.

Can this be achieved OOTB with the Sling Post Servlet?

Greetings.


Solution

  • The solution I'm using is use the @Delete suffix. The problem with it is that you need to add one parameter with the @Delete suffix for every node you want to delete. What I'm doing is query the node in advance to check for all the subnodes of the node I'm updating and adding a @Delete parameter for every one of them.

    So, If the JCR originally has

    -node
     \node1
     \node2
     \node3
    

    I will first get http://example.com/content/node.json and traverse the json. Finally I will send a request with

    ./sling:resourceType:app/component
    ./value:value
    ./node1@Delete
    ./node2@Delete
    ./node3@Delete
    ./node1/value:value1
    ./node2/value:value2
    

    that will update node1 and node2 while deleting node3 at the same time.