Copy Pasting the Page | Community
Skip to main content
Level 6
Sep 8, 2021 8:55 AM UTC
Solved

Copy Pasting the Page

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.
Ronnie09Accepted solution
Level 6
Sep 8, 2021 2:17 PM UTC

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

4 replies

Kiran_Vedantam
New Participant
Sep 8, 2021 12:37 PM UTC

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.

Translate
Ronnie09AuthorAccepted solution
Level 6
Sep 8, 2021 2:17 PM UTC

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

Translate
Asutosh_Jena_
New Participant
Sep 8, 2021 9:54 AM UTC

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.

Translate
Ronnie09Author
Level 6
Sep 8, 2021 10:17 AM UTC

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

Translate