Copy Pasting the Page | Community
Skip to main content
Level 6
September 8, 2021
Solved

Copy Pasting the Page

  • September 8, 2021
  • 2 replies
  • 1169 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Ronnie09

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) {
}
});

2 replies

Asutosh_Jena_
Community Advisor
Community Advisor
September 8, 2021

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.

Ronnie09Author
Level 6
September 8, 2021

@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.

Kiran_Vedantam
Community Advisor
Community Advisor
September 8, 2021

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.

Ronnie09AuthorAccepted solution
Level 6
September 8, 2021

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) {
}
});