Abstract
The Sling API provides a wide range of functionality for interacting with the content repository, so the examples will vary depending on what specific tasks you want to perform.
However, I can provide some examples that demonstrate common use cases of the Sling api with respect AEM.
Creating a node:
ResourceResolver resourceResolver = ...;
Resource resource = resourceResolver.getResource("/content");
ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
properties.put("propertyName", "propertyValue");
resourceResolver.commit();
2. Reading a node:
ResourceResolver resourceResolver = ...;
Resource resource = resourceResolver.getResource("/content/newNode");
if (resource != null) {
ValueMap properties = resource.getValueMap();
String propertyValue = properties.get("propertyName", String.class);
}
3. Updating a node:
ResourceResolver resourceResolver = ...;
Resource resource = resourceResolver.getResource("/content/newNode");
if (resource != null) {
ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
properties.put("propertyName", "newPropertyValue");
resourceResolver.commit();
}
Read Full Blog
Q&A
Please use this thread to ask the related questions.