How to use deepcopyPage function

Siddhant Jain
Contributor
October 3, 2024

Hi I want to know the correct way to use deepCopyFunction for coping one page to another page including the attachment

1 answer

0 votes
Humashankar VJ
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 7, 2024

Hi @Siddhant Jain 

To utilize the deepCopyPage function in Confluence for copying a page, including attachments, follow these steps.

First, obtain a PageManager reference using ComponentLocator. Create a PageCopyOptions object, specifying attachment inclusion, and retrieve references to the original page and destination parent page.

Then, invoke the deepCopyPage function, passing the options, original page, and destination parent page. Ensure the destination parent page exists, and consider handling potential title conflicts, permission issues, and page hierarchy limits.

  •  First, obtain a reference to the PageManager component:

java

PageManager pageManager = ComponentLocator.getComponent(PageManager.class);

  • Create a PageCopyOptions object to specify copy options:

java

PageCopyOptions pageCopyOptions = PageCopyOptions.builder()

    .withAttachments(true)

    .build();

  • Get references to the original page and destination parent page:

java

Page originalPage = pageManager.getPage(originalSpaceKey, originalPageTitle);

Page destinationParentPage = pageManager.getPage(destinationSpaceKey, destinationParentPageTitle);

  • Call the deepCopyPage function:

java

pageManager.deepCopyPage(pageCopyOptions, originalPage, destinationParentPage);

  • Here's a complete example putting it all together:

java

import com.atlassian.confluence.pages.PageManager;

import com.atlassian.confluence.pages.Page;

import com.atlassian.confluence.pages.persistence.dao.bulk.copy.PageCopyOptions;

import com.atlassian.sal.api.component.ComponentLocator;

 

// Get PageManager

PageManager pageManager = ComponentLocator.getComponent(PageManager.class);

 

// Set up copy options

PageCopyOptions pageCopyOptions = PageCopyOptions.builder()

    .withAttachments(true)

    .build();

 

// Get original and destination pages

Page originalPage = pageManager.getPage("SOURCESPACE", "Source Page Title");

Page destinationParentPage = pageManager.getPage("DESTSPACE", "Destination Parent Page Title");

 

// Perform the deep copy

pageManager.deepCopyPage(pageCopyOptions, originalPage, destinationParentPage);

 -------------------------------------------------------------------------

The provided code facilitates the duplication of a Confluence page, including attachments, as a child page under a specified destination parent. Key considerations for successful execution include:

The destination parent page must pre-exist.

The duplicated page retains its original title, potentially necessitating conflict resolution if a similarly titled page already exists.

Adequate permissions are required to perform this operation.

The process may fail due to excessive pages in the hierarchy (default limit: 2000), adjustable by administrators.

Robust exception handling is crucial to mitigate issues such as page non-existence or permission conflicts.

 

Hope this helps - Happy to help further!!
Thank you very much and have a great one!
Warm regards

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.19.19
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events