cancel
Showing results for 
Search instead for 
Did you mean: 

removing children

abruzzi
Champ on-the-rise
Champ on-the-rise
I need to bulk create a bunch of spaces (Alfresco Explorer 3.1.2) that are exactly modeled on other spaces.  Unfortunately the spaces being duplicated are full of stuff, so I was trying to script it.  I was testing a single copy like so:


var newNode = document.copy(document.parent, false);
newNode.name = "1909";


var arrayLength = newNode.children.length;
   
for (var i = 0; i < arrayLength; i++) {

   newNode.removeNode(newNode.children[i]);

}

The newNode by itself contains child associations to the files in the original node.  So the for loop is an attempt to break those association.  However I get the following error:

[blockcode]
16:48:13,982 ERROR [org.alfresco.web.ui.common.Utils] Failed to run Actions due to error: Failed to execute script 'workspace://SpacesStore/e7467042-7167-4b2c-8b07-11a74e348d87': Can't find method org.alfresco.repo.jscript.ScriptNode.removeNode(org.mozilla.javascript.Undefined). (workspace://SpacesStore/e7467042-7167-4b2c-8b07-11a74e348d87#9)
org.alfresco.scripts.ScriptException: Failed to execute script 'workspace://SpacesStore/e7467042-7167-4b2c-8b07-11a74e348d87': Can't find method org.alfresco.repo.jscript.ScriptNode.removeNode(org.mozilla.javascript.Undefined). (workspace://SpacesStore/e7467042-7167-4b2c-8b07-11a74e348d87#9)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:228)
   at org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:187)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   …
[/blockcode]

Any idea what I'm doing wrong?
5 REPLIES 5

kaynezhang
World-Class Innovator
World-Class Innovator
Your code works fine in my environment ,I 'm using alfresco 4.2.
It seems you alfresco version dose not have removeNode method.
<strong>
Can't find method org.alfresco.repo.jscript.ScriptNode.removeNode(org.mozilla.javascript.Undefined)
</strong>

leonardo_celati
Champ in-the-making
Champ in-the-making
If you look at the error, you will find out that the node you are trying to remove is javascript undefined.
For some reason, you are trying to delete a non existing node.

Also, I suppose you want the second parameter to be true to allow a deep copy, which means copy all children.
Well I think this the cause of the problem,

abruzzi
Champ on-the-rise
Champ on-the-rise
Actually I don't want the deep copy–I was hoping that the 'false' would copy the folder without the children.  instead it copies the folder with references to the reference folders children.

I was looking at the undefined being passed to the removeNode method, but to be honest, I've use similar loops to iterate over a series of child nodes without a problem.  I can't honestly tell wether the error is the lack of removeNode method, or a undefined being passed in. I've been working off the javascript API for 3.1 and I'm running 3.1.2 so I'd think the docs are accurate.

Understood, just an idea, shouldn't the loop start from the bottom instead of the beginning ?
Infact it is not clear to me, if you remove the very first node, what happen to the children nodes, are they going to be deleted or just existing as "orphan" ? That could explain the undefined.

arrgh! You're right–an idiot move on my part.  Instead of reversing the for loop, I changed it to a while:


var newNode = document.copy(document.parent, false);
newNode.name = "1909";

while (newNode.children.length>0) {

   newNode.removeNode(newNode.children[0]);

}

thanks for the pointer.

Geof