Get Version History of a page via Java | Community
Skip to main content
Level 3
April 1, 2024
Solved

Get Version History of a page via Java

  • April 1, 2024
  • 3 replies
  • 1627 views

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.


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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

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");

 

 

3 replies

Jagadeesh_Prakash
Community Advisor
Community Advisor
April 1, 2024

@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);
 
Level 3
April 1, 2024

Hi, exception is still encountered.

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



lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
April 1, 2024

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");

 

 

Level 3
April 2, 2024

This one works for me. Thank you so much!

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 2, 2024

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.

 

Level 3
April 2, 2024

Thank you. I will take note of this