Expand my Community achievements bar.

SOLVED

AEM Page version Workflow

Avatar

Level 1

Hi, As part of the requirement we have written a workflow which will automatically create the page version as soon as page content is changed/edited. Now the versions are getting created but the issue is 1)Preview is not working 2) Compare version is not working....but revert to the previous version is working...I have created the launcher on modification of cq:page, and a custom process step with  Below is the code : 

Please provide any suggestions.. Thanks in advance

 

package com.aem.bd.core.workflows;

import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.version.VersionManager;

/**
 * this workflow is used for creation of page versions automatically.
 */
@component(service = WorkflowProcess.class , immediate = true , enabled = true ,
        property={
                "process.label = Page Version Creation Process Step"
        })
public class PageVersionProcessStep implements WorkflowProcess {
    private static final Logger log = LoggerFactory.getLogger(PageVersionProcessStep.class);
    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        String payLoad = workItem.getWorkflowData().getPayload().toString();
        VersionManager versionManager = null;
        try {
            if(payLoad.contains("/jcr:content")){
                payLoad = payLoad.split("/jcr:content")[0];
            }
            payLoad = payLoad+"/jcr:content";
            Node node = workflowSession.getSession().getNode(payLoad);
            String[] string = {"mix:versionable"};
            if(!node.hasProperty("jcr:mixinTypes")) {
                node.setProperty("jcr:mixinTypes", "mix:versionable");
            }
            versionManager = workflowSession.getSession().getWorkspace().getVersionManager();
            versionManager.checkpoint(payLoad);
            log.debug("payload---------"+payLoad);
            workflowSession.getSession().save();
            boolean checkedOut = versionManager.isCheckedOut(payLoad);
            log.debug("checkedOut---------"+String.valueOf(checkedOut));
        } catch (RepositoryException e) {
            throw new RuntimeException(e);
        }
    }
}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ssrikar0811,

I see few issues with general concept:

  • if you will run workflow to create version without locking the page, there is a huge chance that author will make some other changes and you will not have separate version for each change. But if you lock page it it will not be editable for content author for a moment, so keeping in mind that this will happen for every change, authoring will be a hell.
  • creating version for each change is quite heavy process and could consume lot of storage, memory and CPU which could have negative impact on authoring experience, this depends on number of concurrent activates on author instance  

I would suggest to automatically create version only during publication/approval process instead of doing it for each change - in my opinion having version for each change quickly became not manageable and will not bring any value to anyone (business, content authors or technical team).

Regarding page version creation on workflow level, I will first try to use OOTB CreateVersionProcess  workflow process, instead of creating custom one.

create-version.png

If for any reason you have to do implementation on your own, you can try to use PageManager api, like this:

 

 

ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);
if (resourceResolver != null) {
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    if (pageManager != null) {
            Page page = pageManager.getPage("path/to/page");
            if (page != null) {
                pageManager.createRevision(page, "Version title", "Version description");
            }

    }
}

 

Implementing such a step you should avoid setting properties/mixins like versionable by yourself - this should be done automatically by versioning api

View solution in original post

3 Replies

Avatar

Community Advisor

As I understood, versions are getting created but the issue is with preview and comparing versions. can you please check logs and see if you can find any errors? 

Avatar

Community Advisor

@ssrikar0811  Compair the node properties of the version which is working and to the one which is not working. I am expecting that some properties are missing in the node

 

Versions are stored by path '/jcr:system/jcr:versionStorage' in AEM. 

https://experienceleague.adobe.com/docs/experience-manager-64/authoring/siteandpage/working-with-pag...

 

Avatar

Correct answer by
Community Advisor

Hi @ssrikar0811,

I see few issues with general concept:

  • if you will run workflow to create version without locking the page, there is a huge chance that author will make some other changes and you will not have separate version for each change. But if you lock page it it will not be editable for content author for a moment, so keeping in mind that this will happen for every change, authoring will be a hell.
  • creating version for each change is quite heavy process and could consume lot of storage, memory and CPU which could have negative impact on authoring experience, this depends on number of concurrent activates on author instance  

I would suggest to automatically create version only during publication/approval process instead of doing it for each change - in my opinion having version for each change quickly became not manageable and will not bring any value to anyone (business, content authors or technical team).

Regarding page version creation on workflow level, I will first try to use OOTB CreateVersionProcess  workflow process, instead of creating custom one.

create-version.png

If for any reason you have to do implementation on your own, you can try to use PageManager api, like this:

 

 

ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);
if (resourceResolver != null) {
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    if (pageManager != null) {
            Page page = pageManager.getPage("path/to/page");
            if (page != null) {
                pageManager.createRevision(page, "Version title", "Version description");
            }

    }
}

 

Implementing such a step you should avoid setting properties/mixins like versionable by yourself - this should be done automatically by versioning api