Zain Jhan
Zain Jhan

Reputation: 35

Java Script to delete node in alfresco not working

var nodeRef = "workspace://SpacesStore/2112480f-f0e8-461b-9564-78e7225f177c";

try {

var currNode = companyhome.findNode(nodeRef);
getNodeList(currNode);
removeNode(currNode);
}

catch(err) {
logger.log("Exception: " + err.message );
}


function removeNode(node) { 
node.remove();
logger.log(node.displayPath + "/" + node.name + " is deleted");
return true;

                    }

The problem is the node does not get deleted and the logger.log does not log anything...

I used this link for reference http://www.zeenor.com/it/cms/alfresco/380-delete-nodes-documents-in-alfresco-repository-using-javascript-api.html

Any Support is appreciated...

Upvotes: 1

Views: 5754

Answers (2)

Sanket Mehta
Sanket Mehta

Reputation: 622

There is also an OOTB way/webscript to delete node in alfresco:

Http Method: DELETE

http://localhost:8080/alfresco/s/api/archive/workspace/SpacesStore/{NODEREF}

This will delete the content from the actual workspace spacestore.

If you want to purge the deleted content (already deleted node lying inside archive spacestore); use this URL:

http://localhost:8080/alfresco/s/api/archive/archive/SpacesStore/{NODEREF_OF_FILE_FROM_ARCHIVESTORE}

Upvotes: 0

Tahir Malik
Tahir Malik

Reputation: 6643

The code you've copied is not right :).

first you're doing getNodeList(currNode); & then removeNode(currNode);

If I look at the code from the link of getNodeList:

//  List all the nodes under folder
function getNodeList(currNode) {    

    //  Check whether node is document
    if(currNode.isDocument) return removeNode(currNode);

    //  Check whether node is folder
    if (currNode.isContainer) {

        var nodes = currNode.children;
        for (var i=0; i<nodes.length; i++) {            

            if(nodes[i].isDocument) removeNode(nodes[i]);           
            else if (nodes[i].isContainer) getNodeList(nodes[i]);

        }// end for
    }// end if

    return true;
}

Then it's already doing a removeNode() there and after it's being removed in your code you're removing it again.

So or either remove the getNodeList() or removeNode() in your main code. E.g.:

var nodeRef = "workspace://SpacesStore/2112480f-f0e8-461b-9564-78e7225f177c";

try {

var currNode = companyhome.findNode(nodeRef);
//getNodeList(currNode);
removeNode(currNode);
}

catch(err) {
logger.log("Exception: " + err.message );
}

function removeNode(node) { 
logger.log(node.displayPath + "/" + node.name + " is deleted");
node.remove();
return true;
}

Btw, it's bad example to first delete a node and then print the values to the logger.log. So first print them or catch the boolean value if it's removed and then print it like:

function removeNode(node) { 
    if(node.remove())
       logger.log(node.displayPath + "/" + node.name + " is deleted");
    else
       logger.log(node.displayPath + "/" + node.name + " is NOT deleted");
    return true;
    }

Upvotes: 1

Related Questions