I am new to AEM, how can I get workflow current node?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi @Johann_Lu ,
To get the current acting node of a workflow in AEM 6.5 using Java code, you can use the Workflow API provided by AEM. Here is a step-by-step guide on how to achieve this:
Add Dependencies: Ensure you have the necessary dependencies in your Maven project. Add the following dependencies to your pom.xml:
<dependency>
<groupId>com.adobe.granite</groupId>
<artifactId>com.adobe.granite.workflow.api</artifactId>
<version>2.0.28</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
<version>2.20.0</version>
</dependency>
Create a Java Class to Get the Current Workflow Node: Create a Java class that interacts with the Workflow API to get the current node of a workflow.
package com.example.aem.core.workflow;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.Route;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowInstance;
import com.adobe.granite.workflow.exec.WorkflowModel;
import com.adobe.granite.workflow.exec.WorkflowNode;
import com.adobe.granite.workflow.model.WorkflowNodeType;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Session;
@Component(service = WorkflowNodeService.class)
public class WorkflowNodeService {
private static final Logger LOG = LoggerFactory.getLogger(WorkflowNodeService.class);
@Reference
private com.adobe.granite.workflow.WorkflowSession workflowSession;
public WorkflowNode getCurrentWorkflowNode(String workflowInstanceId, ResourceResolver resourceResolver) {
try {
Session session = resourceResolver.adaptTo(Session.class);
WorkflowSession wfSession = workflowSession.adaptTo(WorkflowSession.class);
WorkflowInstance workflowInstance = wfSession.getWorkflow(workflowInstanceId);
for (WorkItem workItem : workflowInstance.getWorkItems()) {
WorkflowNode node = workItem.getNode();
if (node.getType().equals(WorkflowNodeType.PARTICIPANT)) {
return node;
}
}
} catch (Exception e) {
LOG.error("Error getting current workflow node: ", e);
}
return null;
}
}
Use the Service in Your Code: Inject the WorkflowNodeService into your servlet or another OSGi component to use it.
@Component(service = MyWorkflowServlet.class,
immediate = true,
property = {
"sling.servlet.paths=/bin/myworkflow",
"sling.servlet.methods=GET"
})
public class MyWorkflowServlet extends SlingAllMethodsServlet {
@Reference
private WorkflowNodeService workflowNodeService;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
String workflowInstanceId = request.getParameter("workflowInstanceId");
ResourceResolver resourceResolver = request.getResourceResolver();
WorkflowNode currentNode = workflowNodeService.getCurrentWorkflowNode(workflowInstanceId, resourceResolver);
if (currentNode != null) {
response.getWriter().write("Current Node: " + currentNode.getTitle());
} else {
response.getWriter().write("Workflow node not found.");
}
}
}
WorkflowNodeService Class:
MyWorkflowServlet Class:
By following these steps, you can retrieve the current node of a workflow instance in AEM using Java code. This example demonstrates how to set up the necessary dependencies, create a service to interact with the Workflow API, and use this service in a servlet to respond with the current workflow node information.
The details of current WorkItem are in workItem object of execute method of Workflow Process Step.
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
ResourceResolver resourceResolver = null;
If you are looking for current payload(Page/Asset etc), on which workflow is running, the use
String payloadPath = (String) workItem.getWorkflowData().getPayload();
No, I need the current node through workflowID, is it possible?
Could you please provide more details/screenshot for better understanding?
Is it,
Hi @Johann_Lu ,
To get the current acting node of a workflow in AEM 6.5 using Java code, you can use the Workflow API provided by AEM. Here is a step-by-step guide on how to achieve this:
Add Dependencies: Ensure you have the necessary dependencies in your Maven project. Add the following dependencies to your pom.xml:
<dependency>
<groupId>com.adobe.granite</groupId>
<artifactId>com.adobe.granite.workflow.api</artifactId>
<version>2.0.28</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
<version>2.20.0</version>
</dependency>
Create a Java Class to Get the Current Workflow Node: Create a Java class that interacts with the Workflow API to get the current node of a workflow.
package com.example.aem.core.workflow;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.Route;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowInstance;
import com.adobe.granite.workflow.exec.WorkflowModel;
import com.adobe.granite.workflow.exec.WorkflowNode;
import com.adobe.granite.workflow.model.WorkflowNodeType;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Session;
@Component(service = WorkflowNodeService.class)
public class WorkflowNodeService {
private static final Logger LOG = LoggerFactory.getLogger(WorkflowNodeService.class);
@Reference
private com.adobe.granite.workflow.WorkflowSession workflowSession;
public WorkflowNode getCurrentWorkflowNode(String workflowInstanceId, ResourceResolver resourceResolver) {
try {
Session session = resourceResolver.adaptTo(Session.class);
WorkflowSession wfSession = workflowSession.adaptTo(WorkflowSession.class);
WorkflowInstance workflowInstance = wfSession.getWorkflow(workflowInstanceId);
for (WorkItem workItem : workflowInstance.getWorkItems()) {
WorkflowNode node = workItem.getNode();
if (node.getType().equals(WorkflowNodeType.PARTICIPANT)) {
return node;
}
}
} catch (Exception e) {
LOG.error("Error getting current workflow node: ", e);
}
return null;
}
}
Use the Service in Your Code: Inject the WorkflowNodeService into your servlet or another OSGi component to use it.
@Component(service = MyWorkflowServlet.class,
immediate = true,
property = {
"sling.servlet.paths=/bin/myworkflow",
"sling.servlet.methods=GET"
})
public class MyWorkflowServlet extends SlingAllMethodsServlet {
@Reference
private WorkflowNodeService workflowNodeService;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
String workflowInstanceId = request.getParameter("workflowInstanceId");
ResourceResolver resourceResolver = request.getResourceResolver();
WorkflowNode currentNode = workflowNodeService.getCurrentWorkflowNode(workflowInstanceId, resourceResolver);
if (currentNode != null) {
response.getWriter().write("Current Node: " + currentNode.getTitle());
} else {
response.getWriter().write("Workflow node not found.");
}
}
}
WorkflowNodeService Class:
MyWorkflowServlet Class:
By following these steps, you can retrieve the current node of a workflow instance in AEM using Java code. This example demonstrates how to set up the necessary dependencies, create a service to interact with the Workflow API, and use this service in a servlet to respond with the current workflow node information.
It works. Thanks for your help.