Expand my Community achievements bar.

SOLVED

How to get latest published version of the page in code.

Avatar

Level 1

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.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 10

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,

  • you can actually directly check the replication status of the page and get the base version of the page and that would be the published page version. 
    VersionManager versionManager = resourceResolver.adaptTo(VersionManager.class);

    ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class);

    if(replicationStatus.isActivated()) {
    Version baseVersion = versionManager.getBaseVersion(resource.getPath());
    }
  • But, at the same time this might not be the latest version of the page as user might create a version and rollback to an older version of the page and replicate it.

 

Let me know what is the exact requirement and I can help better here.

 

Thanks,

Tanika

 

View solution in original post

4 Replies

Avatar

Level 2

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

Avatar

Level 1

@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?

Avatar

Correct answer by
Level 10

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,

  • you can actually directly check the replication status of the page and get the base version of the page and that would be the published page version. 
    VersionManager versionManager = resourceResolver.adaptTo(VersionManager.class);

    ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class);

    if(replicationStatus.isActivated()) {
    Version baseVersion = versionManager.getBaseVersion(resource.getPath());
    }
  • But, at the same time this might not be the latest version of the page as user might create a version and rollback to an older version of the page and replicate it.

 

Let me know what is the exact requirement and I can help better here.

 

Thanks,

Tanika

 

Avatar

Level 1
Level 1

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