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?
Solved! Go to Solution.
Views
Replies
Total Likes
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
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
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?
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;
}
}
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());
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?
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
Right Aanchal. So, this won't fulfill my requirement then, I need to see some other workaround solution.
Thanks for your suggestions and insights.
Hello @iamnjain -
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?
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