Get content fragment's current version programatically in Sling Model. | Community
Skip to main content
iamnjain
Community Advisor
Community Advisor
July 3, 2023
Solved

Get content fragment's current version programatically in Sling Model.

  • July 3, 2023
  • 3 replies
  • 2329 views

Hello,

 

I have a Content Fragment path authored by Author. We need to have current version of that particular CF. We don't want the previous version's as It's easily available using VersionHistory Interface.

 

How can I achieve this?

 

 

@aanchal-sikka 

Tanika02

Esteban666

rawvarun

Sady_Rifat

MayurSatav

Rohan_Garg

ManviSharma

Ritesh_Mittal

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 Peter_Puzanovs

Hi Ian,

 

Content Fragments implement Versionable API.

 

You can call listVersions[1] on your ContentFragment to list available version.

Then you can use standard Versionable API to get content from the version you need.

 

Regards,

Peter

 

[1] https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/cq/dam/cfm/Versionable.html

 

3 replies

aanchal-sikka
Community Advisor
Community Advisor
July 3, 2023

Hello @iamnjain 

 

When you will access the CF's metadata/content as a Resource or ContentFragment APIs, you would get current Working copy.

 

Requesting you to provide more details, the requirements are not clear

Aanchal Sikka
iamnjain
Community Advisor
iamnjainCommunity AdvisorAuthor
Community Advisor
July 3, 2023

Hello Aanchal

 

So, my requirement is we need to create content which will getting updated in future. We want to leverage CF auto increment version number feature to track each updates on content. So, we want to send current version number along with content to backend system. Is there a way to get current version number of a CF?

Tanika02
Level 7
July 3, 2023

Hello @iamnjain -

 

import com.day.cq.commons.jcr.JcrConstants; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.api.wrappers.ValueMapDecorator; import javax.jcr.Node; import javax.jcr.RepositoryException; public class ContentFragmentVersionUtil { public static int getCurrentVersionNumber(ResourceResolver resourceResolver, String contentFragmentPath) throws RepositoryException { Resource contentFragmentResource = resourceResolver.getResource(contentFragmentPath); Node contentFragmentNode = contentFragmentResource.adaptTo(Node.class); if (contentFragmentNode != null && contentFragmentNode.isNodeType(JcrConstants.MIX_VERSIONABLE)) { javax.jcr.version.Version version = contentFragmentNode.getSession().getWorkspace().getVersionManager().getBaseVersion(contentFragmentNode.getPath()); ValueMap versionProperties = new ValueMapDecorator(version.getFrozenNode().getProperties()); return versionProperties.get(JcrConstants.JCR_VERSIONNUMBER, Integer.class); } return 0; } }
Tanika02
Level 7
July 3, 2023

Hello @iamnjain  - 

 

  • In AEM, the default behavior when working with Content Fragments using the Resource or ContentFragment APIs is to retrieve the current working copy. However, there may be scenarios where you need to access specific versions or historical data, such as auditing, comparing versions, or retrieving specific information from a previous version that's when you can leverage VersionHistory APIs OR ContentFragment APIs
iamnjain
Community Advisor
iamnjainCommunity AdvisorAuthor
Community Advisor
July 3, 2023

Hello @tanika02 

 

Yeah I tried using VersionHistory Interface, but it provides history of all the versions. We want to send current version number along with current content to backend system.

Is there a way to get current version number of a CF?

Peter_Puzanovs
Community Advisor
Peter_PuzanovsCommunity AdvisorAccepted solution
Community Advisor
July 3, 2023

Hi Ian,

 

Content Fragments implement Versionable API.

 

You can call listVersions[1] on your ContentFragment to list available version.

Then you can use standard Versionable API to get content from the version you need.

 

Regards,

Peter

 

[1] https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/cq/dam/cfm/Versionable.html