Groovy Script for copying node and its child node from one path to another
Dear All,
I have written below groovy script for copying whole content and its child content from path (/content/dam/projects/we-retail/communities) to the path (/content/we-retail/pagehub).
My requirement is , I have source path called /content/dam/projects/we-retail/communities and contains lots of child communities with child nodes. And I want to copy all child nodes under /content/dam/projects/we-retail/communities to the destination path /content/we-retail/pagehub

Here my destination path is below.

The issue is here that content is copying in the CRXDE , but pages are coming blank , as shown below.

********************* MY Grovy Script *************************
import org.apache.jackrabbit.commons.JcrUtils
// Groovy Script to create folder pagehubs if it is not present and copy communities content to pagehub
import com.day.cq.commons.jcr.JcrUtil;
String pageHubDestinationPath = "/content/we-retail/pagehub";
if(!getResource(pageHubDestinationPath)){
JcrUtil.createPath(pageHubDestinationPath,"sling:OrderedFolder",session); //create folder pagehub inside we-retail if the folder does not exist.
Resource res = getResource(pageHubDestinationPath);
if(res){
ModifiableValueMap resMVM = res.adaptTo(ModifiableValueMap.class);
resMVM.put("jcr:title","pagehub"); // add title pagehub
println "Resource created: $pageHubDestinationPath";
session.save();
}
}
else{
//if folder pagehub exists no need to create new folder and read all the communities folder and copy inside pagehubs
println "Folder already exist1233: $pageHubDestinationPath"
}
String communitiesSourcePath = "/content/dam/projects/we-retail/communities";
println("session ==== " + session);
Workspace workspace = session.getWorkspace();
Resource communitiesCustomRes = resourceResolver.getResource(communitiesSourcePath);
println("communitiesCustomRes === " + communitiesCustomRes);
if(communitiesSourcePath != null) {
Iterator<Resource> iterator = communitiesCustomRes.listChildren()
while(iterator.hasNext()) {
Resource childResource= iterator.next()
childNode = childResource.adaptTo(Node.class)
String childnodeName = childNode.getName();
//println("childnodeName === " + childnodeName)
String communitiesSourcePath1 = communitiesSourcePath +"/"+ childnodeName;
//println("source path ===== " + communitiesSourcePath1 )
String destinationPath = "/content/we-retail/pagehub/" + childnodeName;
println("destinationPath path ===== " + destinationPath )
workspace.copy(communitiesSourcePath1, destinationPath);
session.save();
}
}