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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,557,125
Community Members
 
Community Events
184
Community Groups

How do we copy pages using ScriptRunner PageManager and deepCopyPage?

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

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.
Aug 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)

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

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events