Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do we copy pages using ScriptRunner PageManager and deepCopyPage?

Lauren Lee
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 14, 2020

Confluence Vers: 7.1.0

Scriptrunner Vers: 6.4.0-p5

Hello, 

I am trying to copy pages based off of an event listener, and I keep getting the following error

 

java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:877) at com.atlassian.confluence.pages.persistence.dao.bulk.DefaultBulkOptions$BaseBuilder.buildDefault(DefaultBulkOptions.java:90) at com.atlassian.confluence.pages.persistence.dao.bulk.copy.PageCopyOptions$Builder.build(PageCopyOptions.java:158) at com.atlassian.confluence.pages.persistence.dao.bulk.copy.PageCopyOptions$Builder$build.call(Unknown Source) at TEST_Archive_Document_2.run(TEST Archive Document 2.groovy:13)

with the code below. 

import com.atlassian.confluence.event.events.content.page.PageEvent
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.AbstractPage
import com.atlassian.confluence.pages.persistence.dao.bulk.copy.PageCopyOptions
import com.atlassian.confluence.pages.persistence.dao.bulk.copy.PageCopyOptions.Builder
import com.atlassian.confluence.pages.persistence.dao.bulk.DefaultBulkOptions
import com.atlassian.confluence.pages.persistence.dao.bulk.DefaultBulkOptions.Builder
import com.atlassian.confluence.pages.persistence.dao.bulk.DefaultBulkOptions.BaseBuilder

def pageManager = ComponentLocator.getComponent(PageManager)
def builderPage = PageCopyOptions.builder()

PageCopyOptions pageCopyOptions = builderPage.build();
Page originalPage = pageManager.getPage(10818720)
Page destinationPage = pageManager.getPage(10818723)
pageManager.deepCopyPage(pageCopyOptions, originalPage, destinationPage)

Additionally, I've tried using the canned CopyTree function, but have had no luck.  

 

 

1 answer

Suggest an answer

Log in or Sign up to answer
1 vote
Ramakrishnan Srinivasan
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.
August 25, 2020

Hi,

Referred link: https://community.developer.atlassian.com/t/setting-a-new-confluence-space-homepage-using-java-api/2842

 

This has worked for me to copy from space "ABC" to space "DEMO".

 

import com.atlassian.confluence.event.events.content.page.PageEvent
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.AbstractPage
import com.atlassian.confluence.pages.persistence.dao.bulk.copy.PageCopyOptions
import com.atlassian.confluence.pages.persistence.dao.bulk.copy.PageCopyOptions.Builder
import com.atlassian.confluence.pages.persistence.dao.bulk.DefaultBulkOptions
import com.atlassian.confluence.pages.persistence.dao.bulk.DefaultBulkOptions.Builder
import com.atlassian.confluence.pages.persistence.dao.bulk.DefaultBulkOptions.BaseBuilder
import com.atlassian.core.util.ProgressMeter
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal
import com.atlassian.confluence.core.DefaultDeleteContext

def currentUser = AuthenticatedUserThreadLocal.get()

def pageManager = ComponentLocator.getComponent(PageManager)


def builderPage = PageCopyOptions.builder()

PageCopyOptions pageCopyOptions = builderPage
                                                                       .withContentProperty(true)
                                                                        .withUser(currentUser)
                                                                        .withProgressMeter(new ProgressMeter())
                                                                         .build();

/*PageCopyOptions pageCopyOptions = PageCopyOptions.builder()
                      .withCopyAttachment(true)
                      .withCopyLabel(true)
                      .withContentProperty(true)
                      .withCopyPermission(true)
                      .withUser(currentUser)
                      .withProgressMeter(new ProgressMeter())
                      .build();*/

 

def originPageTitle = "Some Version - Release Notes"
def destinationPageTitle = "External Reference Pages"

def originalPage=pageManager.getPage("ABC", originPageTitle)
def destinationPage=pageManager.getPage("DEMO", destinationPageTitle)

def toCopyPage = pageManager.getPage("DEMO", originPageTitle)

if(toCopyPage) {
       pageManager.trashPage(toCopyPage, DefaultDeleteContext.DEFAULT)
}

pageManager.deepCopyPage(pageCopyOptions, originalPage, destinationPage)

Aleksey Dzyapko September 22, 2021

@Ramakrishnan Srinivasan would you please explain what exactly does your script? I mean, what for you create/get toCopyPage and then delete it. Looks like for coping page you need only two pages.  Also, I'd appreciate if you tell destinationPage should exist or deepCopyPage function creates it? I came across NullPointerException.
Thanks in advance.

Ramakrishnan Srinivasan
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.
September 22, 2021

@Aleksey Dzyapko 

Confluence does not allow duplicate page title in a given space, "DEMO". I have given a menu to user to copy the page but it is possible user can click that option multiple times at different times.

So, every time when user clicks the copy page, if it was already copied earlier, I delete that and do deepCopyPage again. that is why you see trashPage if toCopyPage exists and then deepCopyPage call. Read "destinationPage" as "destinationParentPage", may be it is more clear. The new copied page is created under destination parent page "External Reference Pages" but the copied page title is same as originPageTitle. sorry, it looks like variable name confusion.

In your case destinationPage should exist because it is a parent where you are copying to, but new copied page title should not exist because you can not duplicate titles.

Hope this explains, if you need more information please feel free to reach out.

Aleksey Dzyapko September 22, 2021

@Ramakrishnan Srinivasan , thanks a lot! You rock!
Also, there is a great option exists withPrefixNameConflictResolver which helps create a page without deleting the previous one in case if need just create a copy.

Siddhant Jain October 3, 2024

can we exclude the Orginal page children to copy at destination Page

DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events