Skip to main content

Posts

Generate id for AEM components

We all know that when we add an AEM component to a page, it creates the DOM structure on the pages based upon our HTL file.

Sometimes we require id attribute to be set in our div for n number of reasons. If we put id attribute in the HTL file itself then it will become static and if in include that component multiple times on the page, the same id will be repeated for all instance; which is not a neat solution.

Wouldn't it be nice if we have a mechanism to generate an id randomly and uniquely for each instance of the component on the page? In this post, we are going to do just that.

Hence, without further ado, let's get started -
Step #1 Create a component that will have the field for unique id. We are making this id editable as well to give more flexibility to the content authors. Below is the cq:dialog.xml file -

<?xml version="1.0" encoding="UTF-8"?><jcr:rootxmlns:sling="http://sling.apache.org/jcr/sling/1.0"xmlns:cq="http://www.d…
Recent posts

Workflows in AEM 05 - Trigger Workflow Programmatically

It is often needed to trigger a workflow programmatically. Hence, in this small post, we will see how we can easily do that.


packageorg.redquark.demo.core.servlets;importjava.io.IOException;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjavax.servlet.Servlet;importorg.apache.sling.api.SlingHttpServletRequest;importorg.apache.sling.api.SlingHttpServletResponse;importorg.apache.sling.api.resource.ResourceResolver;importorg.apache.sling.api.servlets.HttpConstants;importorg.apache.sling.api.servlets.SlingSafeMethodsServlet;importorg.osgi.framework.Constants;importorg.osgi.service.component.annotations.Component;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.adobe.granite.workflow.WorkflowException;importcom.adobe.granite.workflow.WorkflowSession;importcom.adobe.granite.workflow.exec.WorkflowData;importcom.adobe.granite.workflow.model.WorkflowModel;/** * @author Anirudh Sharma */@Component(service=Servlet.class,property={Constants.SERVICE_DESCRIPTION…

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…