Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Siddhant Jain
Contributor
February 19, 2025

Instead of PageManager reference using ComponentLocator why we can't use PageManger constructor 

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.
February 19, 2025

Hi @Siddhant Jain 

We can't use the PageManager constructor directly because PageManager is an interface, not a concrete class.

The Confluence framework provides its implementation at runtime, and ComponentLocator is used to retrieve the properly initialized instance. This ensures that all dependencies are correctly managed within Confluence's service framework.

To learn more - Accessing Confluence Components from Plugin Modules

Hope this helps!!

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