cancel
Showing results for 
Search instead for 
Did you mean: 

JavaScript access to the versionHistory property?

martin_cowie
Champ in-the-making
Champ in-the-making
I'm trying in vain to find a means of examining the version history of a document, using the Javascript API. All I need is the number versions, and the size of each version.

The documentation suggests that FreeMarker can access a nodes 'versionHistory' node, but the JavaScript certainly cannot.

I'm in the process of writing a disk-usage tool, and not having visibility over the document history is a teensy bit of a sticking point 🙂

Can anyone tell me where I'm going wrong?

M.
1 REPLY 1

martin_cowie
Champ in-the-making
Champ in-the-making
Found it! The solution was use the power of the Rhino Javascript engine to access bits of its Java environment.

The ScriptNode object has a protected 'services' field. Once that can be accessed, everything else falls into place …

var PROP_CONTENT = Packages.org.alfresco.model.ContentModel.PROP_CONTENT;
var versionService = null;
var nodeService = null;

function getServices( doc )
{
   // *BEWARE* - HERE BE DRAGONS
   var servicesField = doc.getClass().getDeclaredField( "services" );
   servicesField.setAccessible( true ); // ick - yuck - nasty - but necessary
   var serviceRegistry = servicesField.get( doc );

   versionService = serviceRegistry.getVersionService();
   nodeService = serviceRegistry.getNodeService();
}

function getHistory( doc )
{
   return versionService.getVersionHistory( doc.nodeRef ).getAllVersions().toArray();
}

function getVersionSize( version )
{
   return nodeService.getProperty( version.getFrozenStateNodeRef(), PROP_CONTENT ).getSize();
}

Once getServices() is called with a ScriptNode object, we can then call getHistory() to get a Java array of Version objects. Given a Version object, getVersionSize() can be called to get the size of its content.

It's a bit ugly, but a worthy demonstration of Rhinos power.

M.