R. McDowell
R. McDowell

Reputation: 41

Retrieve the contents of a previous version of an Alfresco NodeRef

I am trying to retrieve the contents of a previous version of an Alfresco NodeRef.

String previousDeployedVersion = "1.3";
VersionHistory history = this.versionService.getVersionHistory(nodeRef);

// Retrieve and display all version labels 

ArrayList<Version> versions = (ArrayList<Version>) history.getAllVersions();

for (Version v1 : versions) 
{
  logger("version = " + v1.getVersionLabel());
}

//// In my test, it displays 1.4, 1.3, 1.2, 1.1, 1.0

Version version = history.getVersion(previousDeployedVersion);
NodeRef previousNode = version.getVersionedNodeRef();

ContentReader contentReader = contentService.getReader(previousNode,
    ContentModel.PROP_CONTENT);

String contentString = contentReader.getContentString();
logger("contentString = " + contentString);

/// At this point it display the content of the current version (1.4) of the NodeRef

Does anyone have any suggestions on how to get the previous version's contents or what I am doing wrong?

Thanks

Upvotes: 1

Views: 765

Answers (1)

Andreas Steffan
Andreas Steffan

Reputation: 6169

Get a frozen state NodeRef from a version. Like so:

VersionHistory vh = versionService.getVersionHistory(nodeRef);
Version version = vh.getHeadVersion();
NodeRef frozenNodeRef = version.getFrozenStateNodeRef();
// version = vh.getPredecessor(version); // Going  back in history
ContentReader contentReader = contentService.getReader(frozenNodeRef,
    ContentModel.PROP_CONTENT);

Upvotes: 2

Related Questions