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,361,270
Community Members
 
Community Events
168
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

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.

@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
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

205 views 1 6
Read article

Atlassian Community Events