How to delete a Folder/Node in CQ repository from a JSP component

Problem:

In one of my projects I needed to show some content nodes in Tree structure in my JSP and then need to delete few of them with inline editing without leaving the page i.e. asynchronously. So I started looking for possible solutions.

Solution:

This blog is about all those findings.

There are two most common ways to manipulate a Node in CQ repository and so for deletion of a node.

 

Using JCR APIs

In this approach, we need to create a custom servlet. We can use javax.jcr.Item interface (http://www.day.com/maven/jsr170/javadocs/jcr-2.0/javax/jcr/Item.html#remove%28%29 ) remove() method inside servlet code to delete a node. Then we can invoke this custom servlet  from our JSP and pass the target node path (which needs to be removed).

Using sling APIs

The SlingPostServlet – using : operation = delete

From HTML/JSP Pages:

<form action=”/node” method=”POST”>

<input name=”: operation”

type=”hidden” value=”delete”>

</form>

OR by using Curl commands (mostly used for testing and deployment script purpose)

To remove existing content just address the item to be removed and set the : operation parameter to delete. For example the following command line removes the /content/sample page:

$ curl -F”:operation=delete” http://host/content/sample

Reference: Manipulating Content: The SlingPostServlet

My Approach-

JCR API approach is more appropriate for my usage but I don’t want to add one more servlet for this simple purpose and started exploring any endpoint which can do Node deletion for me. There I found /bin/wcmcommand for my rescue. This API is less documented so I am not sure if this is the recommended way to achieve my goal. But as far as my knowledge goes it uses the com.day.cq.wcm.api.PageManager interface delete() method internally to delete a node which is an elegant and optimized way to remove any node in JCR repository.

Reference: http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/wcm/api/PageManager.html

Code for delete.jsp : I created a example component called delete.jsp for experimentation

This example has two modes of using /bin/wcmcommand

– Using simple Post method

– Using AJAX : this is what I finally used my case.

<%@include file=”/libs/foundation/global.jsp” %>

<HTML>
<HEAD>
  <TITLE>How to delete a content node from a JSP in CQ authoring environments</TITLE>
</HEAD>
<BODY BGCOLOR=”#FDF5E6″>

<H2>Delete using simple POST operation</H2>
 
  <FORM ACTION=”/bin/wcmcommand”  METHOD=”POST”>
  Node Path to Delete:
  <INPUT TYPE=”TEXT” NAME=”path” VALUE=””>  (e.g. ‘/content/testdelete/deletePage1′)<BR>
  Char-set:
  <INPUT TYPE=”TEXT” NAME=”_charset_” VALUE=”utf-8″><BR>
  Command:
  <INPUT TYPE=”TEXT” NAME=”cmd” VALUE=”deletePage”><BR>
  Force Delete:
  <INPUT TYPE=”TEXT” NAME=”force” VALUE=”false”>(true/false)
  <P>
  <INPUT TYPE=”SUBMIT” value=”Delete Node”  style=”width: 200px”>
              
             
</FORM>

    
 
<H2>Delete via AJAX Call</H2>
   
   Node Path to Delete:
  <INPUT TYPE=”TEXT” ID=”pathAJ” NAME=”pathAJ” VALUE=””>  (e.g. ‘/content/testdelete/deletePage1′)<BR>
  Char-set:
  <INPUT TYPE=”TEXT” NAME=”_charset_” VALUE=”utf-8″><BR>
  Command:
  <INPUT TYPE=”TEXT” NAME=”cmd” VALUE=”deletePage”><BR>
  Force Delete:
  <INPUT TYPE=”TEXT” NAME=”force” VALUE=”false”> (true/false)<BR>
  <P>
  <input TYPE=”button” value=”Delete Node” style=”width: 200px” onclick=”performDelete();”>
              
      <div id=”respText”></div>

</BODY>
    <script type=”text/javascript”>
           
            
    function performDelete() {
        var response = null;
        var url = “/bin/wcmcommand”;
     
        var pathObj= document.getElementById(‘pathAJ’);
        
        if(pathObj)
        {
               
             var params = “path=”+encodeURIComponent(pathObj.value)+”&_charset_=utf-8&cmd=deletePage&force=false”;
             var request = document.all ? new ActiveXObject(“Microsoft.XMLHTTP”) : new XMLHttpRequest();
            //alert(params);
             request.open(“post”, url, false);
             request.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
             request.send(params);
            var resp= request.responseText;
            
            document.getElementById(‘respText’).innerHTML=resp;

        }
        }
            
           
        </script>  
    </HTML>

 

Thanks for your time.

P.S.: This is the solution I found sufficient for my requirements but its not validated or formally recommended from Adobe CQ product team.

Comments are closed.