There is a scenario where i need to copy comments from one page to another when i copy the page. Adaptavist suggested to use scriptrunner plugin for confluence. Can anybody suggest the script to copy the comments. Also i cannot find the event Pagecopy in the scriptrunner event triggers.
Hi Sujatha,
You can use the following script in Script Console. You just need to change the id of the pages you are referring to.
Here's how you can get the page id.
import com.atlassian.confluence.pages.Page import com.atlassian.confluence.pages.Comment import com.atlassian.confluence.pages.PageManager import com.atlassian.sal.api.component.ComponentLocator import com.atlassian.confluence.core.DefaultSaveContext def pageManager = ComponentLocator.getComponent(PageManager) Page initialPage = pageManager.getPage(98311) Page targetPage = pageManager.getPage(98305) for (Comment comment : initialPage.getComments()) { targetPage.addComment(comment) } pageManager.saveContentEntity(targetPage, DefaultSaveContext.MINOR_EDIT)
Regards,
Katy
Adaptavist Product Support
Hi Kelly,
This is a more generic requirement and not specific to a page. Every time i copy a page it should copy the comments.
I want to bind this to the pagecopy event actually, but i cannot find the event trigger to copy page event in the scriptrunner plugin, however i can find oher triggers like pagecreate, pagemove pageupdated etc.
Regards
Sujatha
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sujatha,
If you use the ScriptEventHandlers you'll find the PageCopyEvent. With this event you can get the destination and origin.
Did you try this?
Regards,
Rafael
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That is very strange since it displays fine on mine as you can see:
Which version of ScriptRunner and Confluence you have installed?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sujatha, the PageCopyEvent is only available starting from Confluence 6.x as it can be seen here:
So unfortunately, it will not be available in your Confluence version, you would need to upgrade.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Rafael. We will upgrade and check.
Also how do we get source and destination pages without giving IDS like below
Page initialPage = pageManager.getPage(98311) Page targetPage = pageManager.getPage(98305)
I would want to bind the copyevent with the script which will copy the comments which are open( not resolvd) from source to destination. In this case we will not be able to specify the ID of the source and destination page as it is not known.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The PageCopyEvent contains that, so:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
def event = event as PageCopyEvent def originalPage = event.getOrigin() def destinationPage = event.getDestination()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
import com.atlassian.confluence.event.events.content.page.PageCopyEvent
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Now the script is fine. But the comments are not being copied.
Please check the attachment. I have attached the original and destination document also.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Any update on this? Any reason why the comments are not being copied from one page to another when the page is copied even though we have enabled the script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sujatha,
I just tested and Confluence does not trigger the PageCopyEvent for a single page, it will only trigger if you include the child pages.
There is no way to make this work withou this option set.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
We tried this option by including the child pages while copying. But what happens is, the comments are copied to the new page. However the same has been removed from the parent page. This is not the expectation. The expectation is to copy and not to move the comments, it should be available in both the parent and the child pages.
Regards
Sujatha
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What you need is to create a new comment based on the the source one and add than one instead.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
;;This is the script
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.Comment
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.content.page.PageCopyEvent
def pageManager = ComponentLocator.getComponent(PageManager)
def event = event as PageCopyEvent
def originalPage = event.getOrigin()
def destinationPage = event.getDestination()
for (Comment comment : originalPage.getComments()) {
destinationPage.addComment(comment)
}
pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
import com.atlassian.confluence.core.SaveContext
import com.atlassian.confluence.pages.CommentManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.Comment
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.content.page.PageCopyEvent
def pageManager = ComponentLocator.getComponent(PageManager)
def commentManager = ComponentLocator.getComponent(CommentManager)
def event = event as PageCopyEvent
def originalPage = event.getOrigin()
def destinationPage = event.getDestination()
public static final SaveContext NODATECHANGE_CONTEXT = new DefaultSaveContext(true, false, true)
for (Comment comment : originalPage.getComments()) {
final Comment newComment = new Comment()
newComment.bodyAsString = comment.getBodyAsString()
newComment.creationDate = comment.getCreationDate()
newComment.lastModificationDate = comment.getLastModificationDate()
newComment.creator = comment.getCreator()
commentManager.saveContentEntity(comment, NODATECHANGE_CONTEXT)
destinationPage.addComment(newComment)
}
pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just remove the public static final
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
After removing the public static final, We are getting the above error. Also by the above solution will both inline and page comments be copied?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sujatha,
We cannot tell what your code is since your screenshot only shows half of it.
Also, it shows an error in Line 23. Did you correct that?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
#Below is the script after removing public static line
#error s thrown in the below line
#commentManager.saveContentEntity(comment, #NODATECHANGE_CONTEXT)
variable - NODATECHANGE_CONTEXT is undeclared
#############################################
import com.atlassian.confluence.core.SaveContext
import com.atlassian.confluence.pages.CommentManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.Comment
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.content.page.PageCopyEvent
def pageManager = ComponentLocator.getComponent(PageManager)
def commentManager = ComponentLocator.getComponent(CommentManager)
def event = event as PageCopyEvent
def originalPage = event.getOrigin()
def destinationPage = event.getDestination()
for (Comment comment : originalPage.getComments()) {
final Comment newComment = new Comment()
newComment.bodyAsString = comment.getBodyAsString()
newComment.creationDate = comment.getCreationDate()
newComment.lastModificationDate = comment.getLastModificationDate()
newComment.creator = comment.getCreator()
commentManager.saveContentEntity(comment, NODATECHANGE_CONTEXT)
destinationPage.addComment(newComment)
}
pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.