Hi all,
I am new to AEM and trying to fetch last published version of the page.
Got below code online but versionHistory.getVersions(true).next() thowing error as no such method exists in the class.
// Get the Page Manager service
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
// Get the page that you want to check the published version for
Page page = pageManager.getPage("/content/mysite/en/page1");
// Get the Version Manager service
VersionManager versionManager = resourceResolver.adaptTo(VersionManager.class);
// Get the Version History for the page
VersionHistory versionHistory = versionManager.getVersionHistory(page.getPath());
// Get the current published version of the page
Version publishedVersion = versionHistory.getVersions(true).next();
// Get the version label for the published version
String publishedVersionLabel = publishedVersion.getVersionLabel();
// Print the published version label to the console
log.info("The published version label for the page is: {}", publishedVersionLabel);
Can some help here.
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hello @Racer3189 -
You want to get the current version of the page and additionally want to know if that is a published version or not. If so, basically you want to call it the published version and return the value? Is that the right understanding?
If so,
VersionManager versionManager = resourceResolver.adaptTo(VersionManager.class);
ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class);
if(replicationStatus.isActivated()) {
Version baseVersion = versionManager.getBaseVersion(resource.getPath());
}
Let me know what is the exact requirement and I can help better here.
Thanks,
Tanika
Hi,
The versionHistory.getVersions(boolean) method has been deprecated and removed in AEM 6.5. Instead, you can use the versionHistory.getAllVersions() method and iterate over the Version objects to find the published version.
Here's an updated code snippet that should work:
// Get the Page Manager service
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
// Get the page that you want to check the published version for
Page page = pageManager.getPage("/content/mysite/en/page1");
// Get the Version Manager service
VersionManager versionManager = resourceResolver.adaptTo(VersionManager.class);
// Get the Version History for the page
VersionHistory versionHistory = versionManager.getVersionHistory(page.getPath());
// Iterate over all the versions and find the published version
Version publishedVersion = null;
for (Version version : versionHistory.getAllVersions()) {
if (version.isLive()) {
publishedVersion = version;
break;
}
}
// Get the version label for the published version
String publishedVersionLabel = publishedVersion.getVersionLabel();
// Print the published version label to the console
log.info("The published version label for the page is: {}", publishedVersionLabel);
@ManviSharma1999 , thanks for the reply I have tried above one but I don't see any method version.isLive() available in the class javax.jcr.version.Version please let me know to which package Version class you are referring to?
Hello @Racer3189 -
You want to get the current version of the page and additionally want to know if that is a published version or not. If so, basically you want to call it the published version and return the value? Is that the right understanding?
If so,
VersionManager versionManager = resourceResolver.adaptTo(VersionManager.class);
ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class);
if(replicationStatus.isActivated()) {
Version baseVersion = versionManager.getBaseVersion(resource.getPath());
}
Let me know what is the exact requirement and I can help better here.
Thanks,
Tanika
Hi @Tanika02,
I was trying to get baseVersion of the page with abs page path but it is throwing error that "Node at pagePath is not versionable".
Is there anything extra that is done to make a node versionable, as in the sites console I am able to find versions of that page?
Thanks in advance!
Priyanka
Views
Replies
Total Likes
Views
Likes
Replies