Skip to main content


Workflows in AEM 05 - Trigger Workflow Programmatically

Hey everyone, in the previous posts, we learned about workflows, their different types and how to create them. But in all of the cases discussed earlier, we executed the workflow manually and there may be use cases where one would like to do this automatically or programmatically.

In this article, we will discuss how to trigger an already created workflow model programmatically. We will be using a Sling servlet to perform this task but you can do this from any component, service, etc.

Below are the steps that one should follow while triggering a workflow programmatically -
Get the instance of Resource Resolver.Get the Workflow Session by adapting Resource Resolver instance.Get the Workflow Model which we want to execute.Create an instance of Workflow Data from the workflow session instance passing the payload type and payload.Create a HashMap to set the workflow metadata.Finally, start the workflow passing the workflow model, workflow data and workflow metadata. Codepackageorg.redquar…
Recent posts

Workflows in AEM 04 - MetaDataMap

Sometimes we are faced with a situation where we need to pass data from one workflow step to the another. AEM's workflow API provides an easy way to achieve this using MetaDataMap.

Please note that only primitive data types like Integer, String etc. can be passed from one step to the another. If you have a requirement to pass non-primitive data then use the byte[] array.

Caution: If the data to be passed is too large, refrain passing InputStream. Instead, a better approach is to save the data in a JCR node in the step and retrieve it from the JCR node in a later step.

A MetaDataMap is the data structure which acts as a value map and allows users to set and get data among the steps.
Code example Let's see a code example to demonstrate this concept. Here we will be using two process steps. In the first, we will be passing data and in the second, we will be retrieving it. FirstProcessStep.javapackageorg.redquark.demo.core.workflows;importorg.osgi.service.component.annotations.Com…

Workflows in AEM 03 - Dynamic Participant Step

A participant is a step which enables a user to assign ownership of a task to another user. The workflow will only go further if that user manually acknowledges the step.

A simple use case of this workflow is a review step, i.e. if you have made some changes in an AEM page and then you want someone to review it then you can add it to the participant step and that user will get a notification in his/her inbox. Obviously, the reviewer should have access to the page in question.

Dynamic Participant Step is similar to the Participant Step with the exception that the user is selected at run time. There might be a use case where you want an item to be assigned to a user who has the lowest number of items to review.

The business logic of a Dynamic Participant Step can be written in a class that implements the Participant Chooser interface.

Following is a code example that selects the participant from either "admin" or a user from the "administrators" group based on the c…

Workflows in AEM 02 - Process Step

Hi everyone, it's been a long time since I wrote the first part of this blog. In the previous part, we discussed the fundamentals of workflows, their types and applications.

Today, we are going to discuss the most widely used Workflow step - the process step. This is generally used when we want our application to execute a certain logic.

It executes an ECMA script or an OSGi service to perform automatic processing. A process can be implemented using the following steps -
Create an OSGi service implementing the interface com.adobe.granite.workflow.exec.WorkflowProcess.Set the property process.label. This is the String value by which our workflow needs to be listed.Implement the execute(WorkItem, WorkflowSession, MetaDataMap) method with the implementation code.
The execute() method has three parameters - WorkItem - It is the unit that is passed through a Workflow instance of a WorkflowModel. It contains the WorkflowData. The instances act on and a reference to the WorkflowNode that …

Workflows in AEM 01 - Introduction

Introduction A workflow is a way to automate AEM activities by executing some steps in a specific order to achieve the desired results.

Each step performs an individual activity such as publishing a page, creating a version of the page, sending an email message etc.

For example, the most common activity in AEM is publishing the page from the author instance to the publish instance. But sometimes we want the approval of the changes by some reviewers before publishing. This can be easily achieved by implementing workflows in AEM.

There are many workflows provided out of the box in AEM. Apart from those, if we want, we can also define our custom workflows for our specific activities.
Workflow console The workflow console is the centralized location for workflow management in AEM. There are five tabs in this console - Models: Lists the workflow models that are currently available. We can also create, edit and delete a new workflow here.Instances: This tab shows the details of the currentl…

Sling Servlet 06 - Default and Opting Servlets

Sling ServletsPart 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets
In this post, we are going to discuss two important types of servlets in AEM - Default and Opting servlets, their uses and pros and cons.
Default Servlet A default servlet is selected if no servlet or script for the current resource type can be found. This servlet is registered with a special resource type sling/servlet/default.
The default servlet is resolved exactly the same way as for any resource type. That is, also for the default Servlet selection, the request selectors and extension or method are considered.
Also, the servlet may be a servlet registered as an OSGi service or it may be a script stored in the repository or provided by any bundle.
Finally, if not even a registered default Servlet may be resolved for the request because none has been registered…

Sling Servlets 05: Types of Servlets

Sling ServletsPart 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets Types of Sling Servlets There are two classes in Sling API which can be inherited to define our custom functionality. These two classes are - SlingSafeMethodsServlet []SlingAllMethodsServlet [] Let us discuss these types one by one SlingSafeMethodsServlet This is a helper base class for read-only Servlets used in Sling. This base class is actually just a better implementation of the Servlet API's HttpServlet class which accounts for extensibility.
So extensions of this class have great control over what methods to override.
If any of the default HTTP methods are to be implemented just override the respective doXXX (doGet(), doHead() etc.) method.
This base c…

Sling Servlet 04 - Registration via resourceType or Path

Sling ServletsPart 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets
From the previous post, we know that there are two ways by which a sling servlet can be registered -
Servlet registration via resourceType.Servlet registration via paths. In this post, we will see which approach is better and why. Registration via resourceType A servlet can be registered via resourceType using the following property - "sling.servlet.resourceTypes="+"redquark/servlets/example" Here, whenever the request executes anything with the specified resourceType, the servlet registered with it will be executed. This property can accept a String, an array of Strings or a Vector of Strings. Registration via path A servlet can be registered via path using the following property - "sling.servlet.paths="+"/bin/registeredbypathdemo&…