Get author actual name not userid | Community
Skip to main content
Level 2
April 13, 2023
Solved

Get author actual name not userid

  • April 13, 2023
  • 1 reply
  • 882 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Sady_Rifat

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

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

 

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. 

1 reply

Sady_Rifat
Community Advisor
Community Advisor
April 13, 2023

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-athB1NYLBXvdTuN

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

ashar02Author
Level 2
April 13, 2023

Hi @sady_rifat can give more insight on this i am new to this concept

Sady_Rifat
Community Advisor
Sady_RifatCommunity AdvisorAccepted solution
Community Advisor
April 13, 2023

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

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

 

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.