Expand my Community achievements bar.

SOLVED

Get Version History of a page via Java

Avatar

Level 4

Hi, would like to ask how can I get the all the versions of the page. I've tried the following code

ResourceResolver rr = currentResource.getResourceResolver();
PageManager pageManager = rr.adaptTo(PageManager.class);
Page page = pageManager.getPage(currentResource.getPath());
Session session = rr.adaptTo(Session.class);
VersionManager versionManager = session.getWorkspace().getVersionManager();
VersionHistory versionHistory = versionManager.getVersionHistory(page.getPath());
VersionIterator versionIterator = versionHistory.getAllVersions();
if (versionIterator.hasNext()) {
Version firstVersion = versionIterator.nextVersion();
////////////////Some codes here/////////////
}

 and when it gets to 

VersionHistory versionHistory = versionManager.getVersionHistory(page.getPath());


it would throw an exception

javax.jcr.UnsupportedRepositoryOperationException: Node at /content/acom/us-en/about/sample-page is not versionable.

Reason that I need to get the version history is that I need to get the value of the date for the Version 1.0 of the page. Or are there any other way to do that.

LyonMartin_0-1711952729000.png


*Note: this line of code is added on the Replication/publish action.
Thanks!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @LyonMartin,

In general your code looks correct. The problem is that you are passing path to page, represented by cq:Page node in repository, to make it work you should point to cq:PageContent node which is direct child of your page. According to the documentation getVersionHistory method requires path to node that is versionable, in other case UnsupportedRepositoryOperationException is thrown.

In other words, if your page path is /content/sample/en/page, and you would like to access versions of that page you should use this path /content/sample/en/page/jcr:content

Please change your code as follow:

 

 

// Replace below line in your code
VersionHistory versionHistory = versionManager.getVersionHistory(page.getPath());

// with this
VersionHistory versionHistory = versionManager.getVersionHistory(page.getContentResource().getPath());

// or with this
VersionHistory versionHistory = versionManager.getVersionHistory(page.getPath() + "/jcr:content");

 

 

View solution in original post

6 Replies

Avatar

Community Advisor

@LyonMartin  Below sample code should work. can you give it a try 

 

import javax.jcr.Session;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.version.VersionManager;

// ...

ResourceResolver resolver = // get the resource resolver
Session session = resolver.adaptTo(Session.class);
JackrabbitSession jrSession = (JackrabbitSession) session;
VersionManager versionManager = jrSession.getWorkspace().getVersionManager();

String path = "/content/myPage"; // replace with your page path
String versionName = versionManager.getBaseVersion(path).getName();

System.out.println("Version of the page is: " + versionName);
 

Avatar

Level 4

Hi, exception is still encountered.

javax.jcr.UnsupportedRepositoryOperationException: Node at /content/acom/us-en/sample-page is not versionable



Avatar

Correct answer by
Community Advisor

Hi @LyonMartin,

In general your code looks correct. The problem is that you are passing path to page, represented by cq:Page node in repository, to make it work you should point to cq:PageContent node which is direct child of your page. According to the documentation getVersionHistory method requires path to node that is versionable, in other case UnsupportedRepositoryOperationException is thrown.

In other words, if your page path is /content/sample/en/page, and you would like to access versions of that page you should use this path /content/sample/en/page/jcr:content

Please change your code as follow:

 

 

// Replace below line in your code
VersionHistory versionHistory = versionManager.getVersionHistory(page.getPath());

// with this
VersionHistory versionHistory = versionManager.getVersionHistory(page.getContentResource().getPath());

// or with this
VersionHistory versionHistory = versionManager.getVersionHistory(page.getPath() + "/jcr:content");

 

 

Avatar

Community Advisor

The error you're encountering, javax.jcr.UnsupportedRepositoryOperationException: Node at /content/acom/us-en/about/sample-page is not versionable, indicates that the page you're trying to retrieve the version history for hasn't been made versionable in the JCR (Java Content Repository). In AEM, not all nodes are versionable by default; versioning needs to be explicitly enabled for specific nodes.

To make a node versionable, it must be of a type that supports versioning, typically by including a mixin of type mix:versionable. This is usually handled by AEM for content under /content when you activate (replicate) a page, but there can be cases where this might not occur automatically, or custom content might not be set up to be versionable.

Solutions

  1. Ensure Page Versioning is Enabled:

    • Before fetching the version history, ensure that the page or node in question is versionable. You can check this through the CRXDE Lite by looking for the mix:versionable mixin on the node. If it's not present, the node isn't versionable, and hence you can't retrieve its version history.
  2. Making a Node Versionable (if applicable):

    • This approach requires caution and is generally not recommended for content nodes managed by AEM's Page Manager, as AEM handles versioning automatically for these. Manually modifying node types or adding mixins can have unintended consequences.
  3. Alternative Approach to Retrieve Version Creation Date:

    • If your primary goal is to fetch the creation date of a specific version of a page (e.g., Version 1.0), and direct version management via JCR API proves challenging, consider leveraging AEM's built-in versioning capabilities through the UI or other OOTB (Out of The Box) features. AEM maintains a version history for pages every time they're activated or replicated. This history can be accessed through the timeline feature in the AEM Sites console.
    • For programmatic access, rather than directly using JCR versioning APIs, consider using AEM's APIs or services that abstract these details. However, direct access to version dates programmatically outside of using the JCR API as described might be limited.