How do I get a list of version history using velocity in a user macro.

Yangyang Liu February 25, 2021

I am trying to make a user macro that has a table of strings showing the page version, published date, contributor, and comments.

 

I am trying to use the VersionHistorySummary from the ContentEntityManager interface to get a list of VersionHistorySummary objects. Then use a foreach loop for building the table content.

 

I have tried the velocity template below in the user macro. However, the getVersionHistory is not seems to return anything.

#set($contentManagerClass=$content.class.forName('com.atlassian.spring.container.ContentEntityManager'))

#set($getVersionHistory=$containerManagerClass.getDeclaredMethod('getVersionHistorySummaries'))

2 answers

1 accepted

3 votes
Answer accepted
Dominic Lagger
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.
February 25, 2021

Hi all

I once wrote a really big user macro for something like this. The script gets all childs and its versions. Here is some code sample: 

#set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
#set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
#set( $containerManager=$getInstanceMethod.invoke(null,null) )
#set( $containerContext=$containerManager.containerContext )
#set( $pageManager=$containerContext.getComponent('pageManager') )
#set( $page = $pageManager.getPage($content.getId()) )


###GET ALL VERSIONS FOR FURTHER OPERATIONS
#set( $latestVersions = [] )

#set( $allVersions=[] )
#foreach( $child in $page.getDescendants() )
#set( $childVersions = $pageManager.getVersionHistorySummaries($child.getEntity()) )
#foreach( $childVersion in $childVersions)
#set( $modDate = $childVersion.getLastModificationDate() )
#end
#end

As far as I know, the getVersionHistory is not working. I think, I struggled as well, so I used getVersionHistorySummaries()

Regards, Dominic

Yangyang Liu February 26, 2021

Thanks so much!

It is so helpful!

0 votes
Daniel Törnqvist February 25, 2021

Are you sure that your first call actually returns an ContentEntityManager? 

I'm doing something where I need to retrieve pages based on their pageId and I've tried to get a  ContentEntityManager using both your example above as well as 

#set ( $contentEntityManager = $containerContext.getComponent('contentEntityManager') )

without any luck. 

Maybe someone else has a suggestion?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events