Skip navigation
Currently Being Moderated

Access Users in CRX

Sep 8, 2011 9:22 AM

How can I get the list of users in /home/users in CRX? Is this possible from within the Flex application?

  • Currently Being Moderated
    Sep 8, 2011 12:11 PM   in reply to JPatSoco

    Unfortunately, there aren't any Flex APIs available to get at that info. But it's not hard to create yourself an OSGI Service that does get at it, and expose it using Data Services.


    Here's some code I happened to have lying around that has the bulk of the necessary bits. I used declarative services with the following manifest and XML config files. I keep meaning to convert this to use the Maven SCR plugin, but haven't gotten around to it yet.



    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: User Manager Service
    Bundle-SymbolicName: matt.usermanagerservice.impl
    Bundle-Version: 1.0.0
    Import-Package: org.osgi.framework;version="1.3.0",,
    Export-Package: matt.usermanagerservice.impl;version="0.0.1"
    Bundle-ClassPath: .
    Service-Component: OSGI-INF/usermanagerservice.xml



    <?xml version="1.0" encoding="UTF-8"?>
      <scr:component enabled="true" immediate="true"
        <scr:implementation class="matt.usermanagerservice.impl.UserManagerService"/>
        <scr:service servicefactory="false">
          <scr:provide interface="matt.usermanagerservice.UserManagerService"/>
        <scr:property name="service.description" value="User Manager Service"/>
        <scr:property name="service.vendor"      value="Matt Butler"/>
        <scr:property name=""         value="matt.usermanagerservice.impl"/>
        <scr:property name="" value="UserManagerService"/>
        <scr:reference name="repository"
          cardinality="1..1" policy="static"


    package matt.usermanagerservice.impl;


    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;


    import javax.jcr.AccessDeniedException;
    import javax.jcr.PropertyType;
    import javax.jcr.RepositoryException;
    import javax.jcr.Session;
    import javax.jcr.UnsupportedRepositoryOperationException;
    import javax.jcr.Value;
    import javax.jcr.ValueFormatException;


    import org.apache.jackrabbit.api.JackrabbitSession;


    import matt.usermanagerservice.User;  // A Java bean used to hold User data.. Set it up however you want.


    public class UserManagerService  implements matt.usermanagerservice.UserManagerService
        private SlingRepository repository = null;


        public UserManagerService()




        protected void setRepository(SlingRepository repository) {
            this.repository = repository;
        protected void unsetRepository(SlingRepository repository) {
            this.repository = null;
        // TODO: Look into user since it has better searching APIs and an explicit getEveryone() method. Can be got via JRSession.getPrincipleManager()
        public List<User> findUsers(String propertyNameToFind, String propertyValueToFind)  {
            List<User> users = new ArrayList<User>();
            JackrabbitSession session = null;
            try {
                session = getSession();
                Iterator<Authorizable> userIter = session.getUserManager().findAuthorizables(propertyNameToFind, propertyValueToFind, UserManager.SEARCH_TYPE_USER);
                while (userIter.hasNext()) {
           jackRabbitUser = (;
                    matt.usermanagerservice.impl.User user = new matt.usermanagerservice.impl.User();
                    Map<String, String> properties = new HashMap<String, String>();
                    properties.put("principalName", jackRabbitUser.getPrincipal().getName());
                    populateProperties(jackRabbitUser, ".", properties);
                    populateProperties(jackRabbitUser, "profile", properties);


                    Iterator<Group> memberOfIter = jackRabbitUser.memberOf();
                    List<String> groups = new ArrayList<String>();
                    while (memberOfIter.hasNext()) {
                        Group group =;


                return users;
            } catch (RepositoryException e) {
                // TODO Auto-generated catch block
                return null;
            } catch (CantFindSession e) {
                // TODO Auto-generated catch block
                return null;
            } finally {
                if (session != null)




        private void populateProperties(
                String relativePath,
                String> properties)
                        throws RepositoryException {
            Iterator<String> propertyNameIter = jackRabbitUser.getPropertyNames(relativePath);
            while (propertyNameIter.hasNext()) {
                String propertyName =;
                Value propertyValue = jackRabbitUser.getProperty(relativePath+"/"+propertyName)[0];
                if (propertyValue.getType() != PropertyType.BINARY) { // tends to screw things up.. check it out later.
                    try {
                        properties.put(propertyName, propertyValue.getString());
                    } catch (ValueFormatException vfe) {
                        properties.put(propertyName, "Cannot convert to String");
        private JackrabbitSession getSession() throws CantFindSession {
            String whatAmIDoing = "";
               if (repository == null) {
                throw new CantFindSession("No SlingRepository bound");
            try {
                Session jcrSession = repository.loginAdministrative(repository.getDefaultWorkspace());
                if ( jcrSession instanceof JackrabbitSession)  {
                    org.apache.jackrabbit.api.JackrabbitSession jackrabbitSession = (JackrabbitSession) jcrSession;
                    return jackrabbitSession;
                } else {
                    throw new CantFindSession("Didn't get a JackrabbitSession from repository.loginAdministrative");
            } catch (RepositoryException e) {
                throw new CantFindSession("Failed calling repository.loginAdministrative", e);

    Mark as:
  • Currently Being Moderated
    Sep 12, 2011 6:56 AM   in reply to JPatSoco



    Inside of the Experience Server, you can create Data Services destinations by deploying your code as OSGi bundles. These bundles can export Java Packages - these will then become available for use by the Data Services Message Broker.


    For more information on this, see this Re: Access Users in CRX sample for a step by step overview on creating and deploying bundles to CRX which setup Data Services destinations. Once you have done that, you can try out this sample Re: Access Users in CRX which shows how to retrieve data from the Sample DB via a POJO. (The first sample is a pre-requisite, since it shows the overall approach to getting your code into the Experience Server as bundles)


    You can see the "Creating Data Services applications for the Experience Server" on this page Re: Access Users in CRX for a full set of samples. Also, when you start the Experience Server, you should see a Getting Started link Re: Access Users in CRX which provides more information about developing for ADEP.


    I don't think Matt's sample code would work remotely outside of the CRX container, but can you provide some more information on how you are planning on deploying this?


    Re: Access Users in CRX WSe2a127949a92fa1061747727130427d726e-8000.html

    Re: Access Users in CRX WSe2a127949a92fa10-322b2f813003fea16b-8000.html

    Re: Access Users in CRX index.html

    Re: Access Users in CRX




    Ashish Vashisht

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points