Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add node in AEM?

Tags:

java

service

aem

I need to create a node in AEM using java services. I created a workflow, in which a process triggers a java service inside content/dam/Test.

Do I need to create a node using java services or simply create a node programatically?

like image 771
Mehran Alfin Avatar asked Aug 11 '16 06:08

Mehran Alfin


People also ask

How do I programmatically access the AEM JCR?

You can programmatically modify nodes and properties located within the Adobe CQ repository, which is part of the Adobe Marketing Cloud. To access the CQ repository, you use the Java Content Repository (JCR) API.

How do you set a node property in AEM?

Go to Solution. Node n = session. getNode("/content/project/home"); n. setProperty("Name","AEM"); Node n = session.

What is JCR node in AEM?

The JCR is the base level of the AEM technology stack and is responsible for underlying content persistence, storage, search, access control and much more!


2 Answers

There are various APIs which can be used to create a node :
1. Using Node API

  • Adapt the resource to Node
    Node node = resource.adaptTo(Node.class);
  • then add a node using function "addNode(java.lang.String relPath, java.lang.String primaryNodeTypeName)"
    node.addNode(nodeName, NodePrimaryType);
  • you can add properties using function "setProperty(java.lang.String name,Value value)"
  • Save the session so that the new Node and its properties are saved

  1. Using JcrUtil
    There are 2 APIs for JCRUtil :

    • One of Apache Jackrabbit 2.0 - JcrUtils
    • And the other Utility for common JCR tasks - JcrUtil

You can go through any of them to create a new node.

like image 102
Manisha Avatar answered Oct 16 '22 20:10

Manisha


The workflow session can be adapted to a JCR session, from there you have access to read/write via the JCR API.

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.Constants;

import javax.jcr.Node;
import javax.jcr.Session;

@Component
@Service
@Properties({@Property(name = Constants.SERVICE_DESCRIPTION, value = "Some Service")})
public class AddTheNodeWorkflow implements WorkflowProcess {

    @Override
    public final void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
        try {
            final String payloadPath = getPayloadPath(workItem);
            final Session session = workflowSession.adaptTo(Session.class);

            // get the node for the workflow payload
            final Node payloadNode = session.getNode(payloadPath);

            // add the node
            final Node somenode = payloadNode.addNode("somenode");
            somenode.setProperty("myproperty", "my property value");


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getPayloadPath(WorkItem workItem) {
        return workItem.getWorkflowData().getPayload().toString();
    }
}
like image 27
diffa Avatar answered Oct 16 '22 20:10

diffa