Monday 30 September 2013

Using the Sling API to retrieve content from the Adobe Experience Manager Repository

Adobe Experience Manager contains a Java Content Repository (JCR) that stores nodes and properties. A node located in the JCR is considered a resource. You can use the JCR API to retrieve resources from the JCR. For information about using the JCR API, see Programmatically Accessing Adobe CQ Content using the JCR API.

However, you can also retrieve content from the JCR using Sling. In fact, a resource is a central part of Sling and it assumes everything in the JCR is a resource. You can use Sling to retrieve a resource from within an OSGi component using the Sling Java API or using a JSP. To use the Sling API from within an OSGi component, you use an org.apache.sling.api.resource.ResourceResolverFactory instance.

The Sling API is used within an OSGi bundle


When using Sling to query the JCR, you have access to helper methods that are not available when using the JCR API. For example, the adaptTo method  converts a resource into an appropriate object representing a certain aspect of this resource. For example to translate a Resource object to the corresponding Node object, you can simply do:

Node node = resource.adaptTo(Node.class);

 This development article guides you through how to build an AEM application that uses Sling to retrieve resources from the JCR. An OSGi bundle is created that locates a resource and retrieves a value. To read this development article, click:

http://helpx.adobe.com/experience-manager/using/using-sling-apis.html

Note: When you want to write content to the JCR, it is recommended that you use the JCR API. For information, see Persisting CQ data in the Java Content Repository.

Join the Adobe Experience Cloud Community 

Join the Adobe Experience Cloud Community by clicking this banner




I (Scott Macdonald) am a Senior Digital Marketing Community Manager at Adobe Systems with 20 years in the high tech industry. I am also a programmer with knowledge in Java, JavaScript, C#,C++, HTML, XML and ActionScript. If  you would like to see more CQ or other Adobe Digital Marketing end to end articles like this, then leave a comment and let me know what content you would like to see.


TwitterFollow the Digital Marketing Customer Care team on Twitter @AdobeExpCare.

YouTube: Subscribe to the AEM Community Channel