Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Need help on using userManager.findAuthorizables("jcr:primaryType", "rep:User") in AEM 6.0

Avatar

Level 9

Hi All,

We are trying to get the list of users in cq instance to be displayed in a dropdown.

In the servlet code written, we have the below :

final List<String> users = new ArrayList<String>();

Iterator<Authorizable> iter = userManager.findAuthorizables("jcr:primaryType", "rep:User");

            while (iter.hasNext()) {
                Authorizable auth = iter.next();
                users.add(auth.getID());

            }

 

and we are passing this list to the below method, to create a json response.

public JSONArray createJson(List<String> users) {
        JSONObject eachOption = null;
        JSONArray optionsArray = new JSONArray();
        try {
            if (users != null && users.size() > 0) {
                for (int i = 0; i < users.size(); i++) {
                    eachOption = new JSONObject();
                    eachOption.put("text", users.get(i));
                    eachOption.put("value", users.get(i));
                    optionsArray.put(eachOption);
                }
            }
        } catch (Exception e) {
            LOG.error("createJson() " + e.getMessage());
        }
        return optionsArray;
    }

 

1] In the response returned for "text" and "value" above we are getting only the email values of users, not the user name.

2] What should be done to get the user names, instead of email values.

1 Accepted Solution

Avatar

Correct answer by
Level 10

I am working on a custom tool that does a similar task. I am locating users in AEM under a given path.  The number of users is unknown and I am using a date search. 

However - i am using JCR SQL2 - not UserManager API to locate the given User nodes. Here is example:

//Set up a JCR-SQL2 date search - between Dec 1-and Dec 15 and using jcr_created prop

//all users stored under /home/users/users

  String sqlStatement = "select * from [rep:User] where isdescendantnode('/home/users/users') and [jcr:created] > cast('2015-12-01T00:00:00.000+02:00' as date) and [jcr:created] < cast('2015-12-15T23:59:59.000+02:00' as date)";

  javax.jcr.query.Query query = queryManager.createQuery(sqlStatement,"JCR-SQL2");

   //Execute the query and get the results ...
   javax.jcr.query.QueryResult result = query.execute();

    //Iterate over the nodes in the results ...
     javax.jcr.NodeIterator nodeIter = result.getNodes();

            long mySize = nodeIter.getSize();
            allNodes =(long) mySize;

 

            while ( nodeIter.hasNext() ) {

                //For each node-- get the path of the node

                javax.jcr.Node node = nodeIter.nextNode();

                String myPath = node.getPath();

                //node represents the a Rep:user node

                //you can either read nodes or use Path prop to drill down for children nodes that contain user props

                }

Now if your implementation stores user props on the node - you can get the props. If your implementation stores user profile on a sub nodes - you can use this path and get the sub node to read the user properties. 

To get node properties - simply call the node.getProperty method (ie - to get name prop)

name = node.getProperty("name").getString() ;

Iterate through the result set and read all props. Place the user props in an ArrayList - soimething like:

ArrayList<members> memberList

where members is a Java POJO that has user getter and setter methods. 

THen once you read through all of your nodes the memberList collection will contain all of your user information that you read for user nodes. 

"What should be done to get the user names, instead of email values."

Read the correct props on the user node. Read the name prop. 

Hope this helps. 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

I am working on a custom tool that does a similar task. I am locating users in AEM under a given path.  The number of users is unknown and I am using a date search. 

However - i am using JCR SQL2 - not UserManager API to locate the given User nodes. Here is example:

//Set up a JCR-SQL2 date search - between Dec 1-and Dec 15 and using jcr_created prop

//all users stored under /home/users/users

  String sqlStatement = "select * from [rep:User] where isdescendantnode('/home/users/users') and [jcr:created] > cast('2015-12-01T00:00:00.000+02:00' as date) and [jcr:created] < cast('2015-12-15T23:59:59.000+02:00' as date)";

  javax.jcr.query.Query query = queryManager.createQuery(sqlStatement,"JCR-SQL2");

   //Execute the query and get the results ...
   javax.jcr.query.QueryResult result = query.execute();

    //Iterate over the nodes in the results ...
     javax.jcr.NodeIterator nodeIter = result.getNodes();

            long mySize = nodeIter.getSize();
            allNodes =(long) mySize;

 

            while ( nodeIter.hasNext() ) {

                //For each node-- get the path of the node

                javax.jcr.Node node = nodeIter.nextNode();

                String myPath = node.getPath();

                //node represents the a Rep:user node

                //you can either read nodes or use Path prop to drill down for children nodes that contain user props

                }

Now if your implementation stores user props on the node - you can get the props. If your implementation stores user profile on a sub nodes - you can use this path and get the sub node to read the user properties. 

To get node properties - simply call the node.getProperty method (ie - to get name prop)

name = node.getProperty("name").getString() ;

Iterate through the result set and read all props. Place the user props in an ArrayList - soimething like:

ArrayList<members> memberList

where members is a Java POJO that has user getter and setter methods. 

THen once you read through all of your nodes the memberList collection will contain all of your user information that you read for user nodes. 

"What should be done to get the user names, instead of email values."

Read the correct props on the user node. Read the name prop. 

Hope this helps.