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: 



Adobe Digital Marketing Community

Join the Adobe Digital Marketing Community. Start by clicking this banner

About the Author

I (Scott Macdonald) am a Senior Digital Marketing Community Manager at Adobe Systems with over 16 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.

Linked Inhttp://www.linkedin.com/in/scottmacdonald2010
TwitterFollow the Digital Marketing Customer Care team on Twitter @AdobeMktgCare.



7 comments:

  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);
    logger.info("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);
    logger.info("resolver ::: "+resolver);
    Session session = resolver.adaptTo(Session.class);
    logger.info(session.toString());

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

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

    ReplyDelete
    Replies
    1. IN the Sling mapping - specify the
      Bundle-SymbolicName:

      com.community.aem.slingjcr-bundle

      Delete
  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"

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

    http://stackoverflow.com/questions/31350548/resourceresolverfactory-getserviceresourceresolver-throws-exception-in-aem-6-1

    ReplyDelete