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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 Confluence Cloud

🎨 Add some visual life to your templates

Hi Atlassian Community, My name is Avni Barman, and I am a Product Manager on the Confluence Cloud team. Based on feedback from you, we are giving admins more power to create templates that a...

183 views 3 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you