Expand my Community achievements bar.

Version Creation after deployed bundle into package manger AEM6.5

Avatar

Level 2

Hi,
We are using AEM 6.5

I have a requirement on new version creation in AEM Page Version Creation/history, when I deploy a bundle (having some new content/image) in package manager the corresponding page should create a new version, is it possible?

Example
1. I'm not going to manually edit some content on the page when I deploy the bundle in the package manager, can we get the version history.
2. Suppose i did some changes in Dev env at a page abc.html and page have versions up to  V1,V2 and V3 then made a package through package manager(versionupgrade.zip)  then i deployed into other env once i deployed into package manager i want to get the new version with updated content, which i have deployed the content from lower env..

sivav84492733_0-1718205061937.png

sivav84492733_2-1718205138304.png

 

8 Replies

Avatar

Community Advisor

HIi @siva_vjwd 

There is no out-of-the-box way to automatically trigger page versioning when deploying packages with new content.

This requires custom development, involving creating an OSGi service, analyzing package contents to map resources to pages, and invoking the PageManager API to create versions for affected pages when a package is deployed.

Avatar

Community Advisor

Hi @siva_vjwd 

 

As @gkalyan mentioned, it is not available OOTB, but you can achieve this through workflows. I will try to outline the steps you can follow to create page versions on package installation - 

  1. Create a new workflow model with "Create Version" step
  2. Add a new workflow launcher with below details
    • Event Type = Created
    • Nodetype = cq:Page
    • Path = <your-content-path>
    • Run Mode = Author
    • Workflow Model = workflow created in step#1
  3. Repeat Step#2 with Event Type = Modified 

 

I highly recommend enabling the Version Purge configuration and retain only certain number of versions to avoid the performance impacts in environments. You can refer to this document for reference - https://experienceleague.adobe.com/en/docs/experience-manager-65/content/implementing/deploying/conf... 

 

Thanks,

Lokesh 

 

Avatar

Community Advisor

Hi @siva_vjwd ,

To automatically create a new version of a page in AEM 6.5 when you deploy a bundle via the Package Manager, you will need to implement a custom event handler that listens for package installation events and then triggers the versioning of the relevant pages.

Here’s a step-by-step guide on how you can achieve this:

1. Implement an Event Listener

Create an event listener that listens for package installation events. This listener will then trigger the versioning process for the pages affected by the package deployment.

Example Code for Event Listener

  1. Create an Event Listener in OSGi:

 

package com.example.aem.core.listeners;

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.apache.sling.event.jobs.consumer.JobConsumer.JobResult;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationEvent;

import javax.jcr.Node;

@Component(
        service = JobConsumer.class,
        property = {
                JobConsumer.PROPERTY_TOPICS + "=" + ReplicationEvent.EVENT_TOPIC
        }
)
public class PackageInstallEventListener implements JobConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(PackageInstallEventListener.class);

    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    @Override
    public JobResult process(Job job) {
        String path = (String) job.getProperty(ReplicationAction.PROPERTY_PATH);

        try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(null)) {
            PageManager pageManager = resolver.adaptTo(PageManager.class);
            if (pageManager != null) {
                Page page = pageManager.getPage(path);
                if (page != null) {
                    Node pageNode = page.adaptTo(Node.class);
                    if (pageNode != null) {
                        pageNode.getSession().getWorkspace().getVersionManager().checkpoint(pageNode.getPath());
                        LOG.info("Version created for page: {}", path);
                    }
                }
            }
        } catch (Exception e) {
            LOG.error("Error creating version for page: {}", path, e);
            return JobResult.FAILED;
        }
        return JobResult.OK;
    }
}
​

 

2. Configure the Event Listener

Ensure the event listener is configured to respond to package installation events. This usually involves setting up the appropriate event topics and filters.

3. Ensure Service User Mapping

Make sure that your service user mapping is correctly set up for your bundle to have the necessary permissions to create versions of pages.

Example Configuration for sling.serviceUser.mapping:

In your org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended-serviceUserMapping configuration:

 

{
    "user.mapping": [
        "com.example.aem.core.listeners:your-service-user"
    ]
}

 

4. Deploy and Test

Deploy your bundle and test the functionality:

  1. Install a package via the Package Manager that affects specific pages.
  2. Verify that the event listener is triggered and a new version is created for the affected pages.

 

  • Event Listener: An OSGi event listener that listens for package installation events and triggers the version creation for affected pages.
  • Configuration: Properly configure the listener and service user mapping to ensure the necessary permissions.
  • Testing: Verify the functionality by deploying a package and checking the version history of the affected pages.

This approach ensures that every time a package is deployed that affects specific pages, a new version is automatically created, meeting your requirement for automated versioning without manual intervention.




Avatar

Level 7

Thanks ChatGPT, man you need to stop spamming with these ai, CHATGPT, comments...

Avatar

Community Advisor

Hi @siva_vjwd 
Are these images/pages are deployed from maven project with build & deploy and are part of ui.content package?

 



Arun Patidar

Avatar

Level 2

No, we are code deploying, not from maven project. 

Avatar

Administrator

@siva_vjwd Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni