AEM Programmatically Write/Update/Modify AEM JCR node property/properties

In this article, I will be presenting code snippets for how we can programmatically update AEM JCR nodes in AEM using Java; using the org.apache.sling.api.resource.ModifiableValueMap and javax.jcr.Node API.

In this article, we are showcasing the way that you would programmatically update AEM JCR nodes in your local machine, logged in as a System Users in AEM. If you would like how to setup your system user programmatically with code configurations, read this article here.

Exploring AEM Service Users: A Simple Setup Guide
Dive into the world of Adobe Experience Manager with our blog post, “Exploring AEM Service Users.” Whether you’re on AEMaaCS or AEM 6.5.4+, we’ve got you covered with an easy-to-follow guide on setting up a system user. No jargon, just straightforward steps to demystify the AEM service user. Enhance your AEM experience effortlessly – check out our blog and learn today -> https://sourcedcode.com/blog/aem/create-system-service-users-in-aem-6-5-with-code-configurations


Method 1, Programmatically Update AEM JCR nodes in JAVA with org.apache.sling.api.resource.ModifiableValueMap

Take a look at line 7,8 where we adapt the resource object to ModifiableValueMap.class, make the change in the existingProperty using the ModifiableValueMap API, put… In line 9, we are updating an existing property, and in line 10, we are removing an existingProperty. Finally, in line 11, using the resolver to commit the changes. The commit method will Persist all pending changes into the JCR, documentation here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
final Map<String, Object> authInfo = Collections.singletonMap(
        ResourceResolverFactory.SUBSERVICE,
        "sourcedCodeSystemUser");
try (ResourceResolver resolver = factory.getServiceResourceResolver(authInfo)) {
    Resource resource = resolver.getResource("/content/sourcedcode/jcr:content");
    if (Objects.nonNull(resource)) {
        ModifiableValueMap map = resource.adaptTo(ModifiableValueMap.class);
        map.put("newProperty", "value");
        map.put("existingProperty", "value");
        map.remove("existingProperty"); //remove
        resolver.commit();
    }
} catch (Exception e) {
    e.printStackTrace();
}
try-with-resources statement line:4
The try-with-resources statement is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource. reference documentation.

Method 2, Programmatically Update AEM JCR nodes in JAVA with, javax.jcr.Node

Take a look at line 6, where we will get the JCR node as a javax.jcr.Node object, in line 7 we will check if that Node exists, and line 8, we create a new property. On line 9, we will update an existing property, and in line 10, we will remove an existing property, in line 11, we will save all changes into the repository.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
final Map<String, Object> authInfo = Collections.singletonMap(
        ResourceResolverFactory.SUBSERVICE,
        "sourcedCodeSystemUser");
try (ResourceResolver resolver = factory.getServiceResourceResolver(authInfo)) {
    Session session = resolver.adaptTo(Session.class);
    Node contentNode = session.getNode("/content/sourcedcode/jcr:content");
    if (Objects.nonNull(contentNode)) {
        contentNode.setProperty("newProperty", "value");
        contentNode.setProperty("existingProperty", "value");
        contentNode.setProperty("existingProperty", (Value)null); //remove
        session.save();
    }
} catch (Exception e) {
    e.printStackTrace();
}

This article covers questions like

  • How to modify AEM JCR node property/properties programmatically?
  • Modify AEM JCR node property/properties programmatically with code
  • Use AEM Sling API to modify AEM JCR node property/properties
  • Use AEM Node JCR to modify AEM JCR node property/properties

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

One thought on “AEM Programmatically Write/Update/Modify AEM JCR node property/properties

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top