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

Get author actual name not userid

Avatar

Level 3

I have requirement to get author name who has created the page. I am able to get fetch pageProperties.jcr:createdBy but its giving me author user Id. I want the actual name of the author(full name). How we can achieve this.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

This is an example User profile. The user ID is set in pageproperties jcr:createdBy 

Sady_Rifat_0-1681388064355.png

This is the user data in CRX stored. You need to fetch the user familyName and givenName.

Sady_Rifat_1-1681388189460.png

 

Now when this user is creating a page userID will place in pageProperties.jcr:createdBy

Read this user ID and place it in Java code

UserManager userManager = request.getResourceResolver().adaptTo(UserManager.class);
Authorizable authorizable = userManager.getAuthorizable("userID");

Now actually you will know the actual user profile path by 

authorizable.getPath()

Now resolve the resource and get the necessary values: 

String fName = request.getResourceResolver().getResource(authorizable.getPath() + "/profile").getValueMap().get("givenName", "");
String lName = request.getResourceResolver().getResource(authorizable.getPath() + "/profile").getValueMap().get("familyName", "");

 

Note: You might not have access to read value under  /home/users/ except for the administrator user role. In that case, you can create a service user instead of request.getResourceResolver() use only resourceResolver which has the permission of the service user. 

View solution in original post

3 Replies

Avatar

Community Advisor

Hello you can do it in 2 ways:

Follow this document: https://www.albinsblog.com/2015/03/how-to-get-userinfo-through-java-api-in.html#.ZDfbs9JBxH4 

Or,

You can use this code snippet

 

UserManager userManager = request.getResourceResolver().adaptTo(UserManager.class);
Authorizable authorizable = userManager.getAuthorizable("userIDFromCRX");
String fName = request.getResourceResolver().getResource(authorizable.getPath() + "/profile").getValueMap().get("givenName", "");

 

 

User: http://localhost:4502/libs/granite/security/content/v2/usereditor.html/home/users/we-retail/DSCP-ath...

User data in CRX: /home/users/we-retail/DSCP-athB1NYLBXvdTuN

Avatar

Correct answer by
Community Advisor

This is an example User profile. The user ID is set in pageproperties jcr:createdBy 

Sady_Rifat_0-1681388064355.png

This is the user data in CRX stored. You need to fetch the user familyName and givenName.

Sady_Rifat_1-1681388189460.png

 

Now when this user is creating a page userID will place in pageProperties.jcr:createdBy

Read this user ID and place it in Java code

UserManager userManager = request.getResourceResolver().adaptTo(UserManager.class);
Authorizable authorizable = userManager.getAuthorizable("userID");

Now actually you will know the actual user profile path by 

authorizable.getPath()

Now resolve the resource and get the necessary values: 

String fName = request.getResourceResolver().getResource(authorizable.getPath() + "/profile").getValueMap().get("givenName", "");
String lName = request.getResourceResolver().getResource(authorizable.getPath() + "/profile").getValueMap().get("familyName", "");

 

Note: You might not have access to read value under  /home/users/ except for the administrator user role. In that case, you can create a service user instead of request.getResourceResolver() use only resourceResolver which has the permission of the service user.