Thursday, 4 December 2014

Querying Adobe Experience Manager 6 data using the Sling getServiceResourceResolver method

You can create an Adobe Experience Manager (AEM) 6 application that queries data located in the AEM Java Content Repository (JCR). To query data, you use a javax.jcr.query.Query instance that belongs to the JCR Query API. This API supports both searching and querying operations. For example, assume that your AEM application tracks your organization’s customers. You can query the JCR to obtain a customer result set in which a digital marketer is interested.

As of AEM 6, you get a session instance (required to work with the JCR API) by using a Sling method named getServiceResourceResolver(). This new method replaces the use of the following API code:

 ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
  session = resourceResolver.adaptTo(Session.class);

 The following code shows use of the more secure getServiceResourceResolver API call.

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
ResourceResolver resolver = null;

try {
    //Invoke the adaptTo method to create a Session used to create a QueryManager
resolver = resolverFactory.getServiceResourceResolver(param);
    session = resolver.adaptTo(Session.class);

This code uses a specific AEM account with JCR read and write privileges to access the AEM 6 JCR.

Note: As of AEM 6, the getAdministrativeResourceResolver method is deprecated.

This article creates the following AEM Query application.

To watch the video, click: 

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


  1. Hi, I was trying to get the session object, but its giving me error like : error is Login Failure: all modules ignored. Below is my code snippet :
    SlingScriptHelper sling = (SlingScriptHelper)getJspContext().getAttribute("sling");
    ResourceResolverFactory resolverFactory = sling.getService(ResourceResolverFactory.class);"resolverFactory ::: "+resolverFactory);
    Map param = new HashMap();
    param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
    ResourceResolver resolver = null;
    //Invoke the adaptTo method to create a Session used to create a QueryManager
    resolver = resolverFactory.getServiceResourceResolver(param);"resolver ::: "+resolver);
    Session session = resolver.adaptTo(Session.class);;

  2. Can any one please provide solution for the above comment. I also ran into same issue.

  3. Did you map the user as shown in the article?

    1. IN the Sling mapping - specify the

  4. Does the same work on AEM 6.1?
    The same code works for me in AEM 6.0 but not in AEM 6.1.

    The resolver = resolverFactory.getServiceResourceResolver(param);
    always throws a Login Exception

    "Cannot derive user name for bundle group.tti.commons-service [395] and sub service writeService"

  5. I added the solution for AEM 6.1 here. Hope it helps!