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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

Copy Pasting the Page

Avatar

Level 6

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 6

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 6

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

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

The ultimate experience is back.

Join us in Vegas to build skills, learn from the world's top brands, and be inspired.

Register Now