Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Copy Pasting the Page

Ronnie09
Level 3
Level 3

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
Ronnie09
Correct answer by
Level 3
Level 3

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
asutosh_jena
Community Advisor
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.

Ronnie09
Level 3
Level 3

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

KiranVedantam1992
Community Advisor
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.

Ronnie09
Correct answer by
Level 3
Level 3

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