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
- 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:
- Install a package via the Package Manager that affects specific pages.
- 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.