Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Community Advisor

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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/...

 

View solution in original post

11 Replies

Avatar

Community Advisor

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

Avatar

Community Advisor

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?

Avatar

Community Advisor

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

Avatar

Community Advisor

Hello @Tanika02 

 

I tried above code snippet. But there's no JCRVERSIONNUMBER variable in

com.day.cq.commons.jcr.JcrConstants

 

Is this updated?

Avatar

Community Advisor

@iamnjain 

 

It appears that version number and the relevant content is important for your system. Few observations that I wanted to share:

 

1. On Edit, a persisted version is created on the Edit of content. Thats the once visible in Timewrap. The content in this version is all content before user started edits. 

2. All other auto-saved changes are not persisted versions. They are only added to current working copy.

3. When a user saves from UI, the changes are still reflected in current working copy, but they are NOT part of any version.

4. They become part of an incremented version only on next edit. Or If user creates a version explicitly

 

Just highlighting the above points, because edited data that you retrieve from Asset using API during step-3 and step-4, are not part of any version.

 

In your case, you would need to get the Latest version and pass-on info from that version to external system. Or the user would need to create a version after making changes.

 

Please review once and do suggest if you observe otherwise.

 

Getting the latest version from API would be as follows

 

VersionManager versionManager = resolver.adaptTo(Session.class).getWorkspace().getVersionManager();
Version baseVersion = versionManager.getBaseVersion(resource.getPath());

 


Aanchal Sikka

Avatar

Community Advisor

Hello @aanchal-sikka 

 

I agree on your observations.

My requirement is to get the current version number and pass it on to external system along with content.

 

When I checked further, I found out that Content Fragment shows Current as latest (present) version in Touch UI Timeline section.

In CRX, it stores as jcr:rootVersion node and it does not contain a version number.

 

When we make a edit and save, then a current version marked with a version number and moved to versionHistory labels and new current version stored as jcr:rootVersion node.

 

Correct me, if my understanding is right?

Avatar

Community Advisor

hello @iamnjain 

 

The Current that we see in in timeline, is the current working copy.  Its the CF under /content/dam, not a version.

 

When we Edit and Save, again no version is created OOTB.

 

When we Edit the CF next time, that a version is created of the last changes done by an author. Or an author can create a version explicitly after his changes are complete.

 

What you might be interested in are these numbers 1.0, 1.0.0, 1.1 and 1.2. Each correspond to a version and are version numbers for an asset

aanchalsikka_0-1688446444571.png

 

 


Aanchal Sikka

Avatar

Community Advisor

Right Aanchal. So, this won't fulfill my requirement then, I need to see some other workaround solution.

 

Thanks for your suggestions and insights.

Avatar

Community Advisor

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

Avatar

Community Advisor

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?

Avatar

Correct answer by
Community Advisor

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/...