Expand my Community achievements bar.

SOLVED

Copy Pasting the Page

Avatar

Level 7

I want to copy and paste the page even if it has any child pages. If I have copy pasted the page successfully then rerunning that workflow should overwrite the copied page if page is already present. 


Current approach:-- 

public void copyPage(String productPagesRootPath, ResourceResolver resourceResolver) {
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page page=pageManager.getPage(productPagesRootPath);
Resource parent = resourceResolver.resolve(page + "/target");
try {
if (parent.getResourceType().equals(Resource.RESOURCE_TYPE_NON_EXISTING)) {
pageManager.create(productPagesRootPath, "target", "Template", "Copy_Temp");
}
pageManager.copy(page, productPagesRootPath+ "/target/newPage", null, true, false);
} catch (WCMException e) {
LOG.error("Error in Copying Page ", e.getMessage(), e);
}
}

 

This is running good but whenever I rerun the code it doesn't overwrite but pops an error. If possible I also want to copy the page in the same path. Above code is creating child page /target/newPage.

Please optimize the approach as well. 
Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Level 7

Yeah, it exist. I fixed the code now it is working fine

try {

List<String> list;

list.forEach(listItem -> {
Resource resource = resourceResolver.getResource(listItem);
PageManager pageManager=resourceResolver.adaptTo(PageManager.class);
Page page=pageManager.getPage(getResourceFromPayload(WorkItem).getPath());
pageManager.copy(page,getResourceFromPayload(WorkItemtem).getPath(),resource.getName(),true,true);
} catch (WCMException e) {
}
});

View solution in original post

4 Replies

Avatar

Community Advisor

Cloud you please add some more information here. I am not clear with the ask. When you say you want to copy paste you want to do it via code? Why is it required as I believe copy paste should be a one time process and can be done manually.

Avatar

Level 7

@Asutosh_Jena_ I am writing workflow for it, because for some we have more than 100 pages. Can't do it manually.

 

I just want to copy paste with different name.... If copy is available then just Overwrite that copy.

Avatar

Community Advisor

Hi @Ronnie09,

 

In your code below, you are creating the page only when resourceType = RESOURCE_TYPE_NON_EXISTING.

 

if (parent.getResourceType().equals(Resource.RESOURCE_TYPE_NON_EXISTING)) {
pageManager.create(productPagesRootPath, "target", "Template", "Copy_Temp");
}

 

Can you check if the page that you have created also exists and run the workflow?

 

Thanks,

Kiran Vedantam.

Avatar

Correct answer by
Level 7

Yeah, it exist. I fixed the code now it is working fine

try {

List<String> list;

list.forEach(listItem -> {
Resource resource = resourceResolver.getResource(listItem);
PageManager pageManager=resourceResolver.adaptTo(PageManager.class);
Page page=pageManager.getPage(getResourceFromPayload(WorkItem).getPath());
pageManager.copy(page,getResourceFromPayload(WorkItemtem).getPath(),resource.getName(),true,true);
} catch (WCMException e) {
}
});