Use UserUtils in a velocity template

Hello,

I'm trying to retrieve the full name of some users from their username. I think the UserUtil interface can do that but I couldn't find how to make it works in my vm file.

My velocity template:

#set($name = $userutil.getUserByName("bla").getDisplayName())
...
$name

Problem: "$name" is displayed but not replaced by the full name.

My atlassian-plugin.xml:

<component-import key="userutil" 
        interface="com.atlassian.jira.user.util.UserUtil" />

JIRA version: v6.3.3

Thanks for your time and your help.

2 answers

1 accepted

This widget could not be displayed.

using $userutil assumes that you've got getUserUtil in your webwork action which renders the velocity template.

So you can either expose UserUtil through a getter in the java code or create a method that calls UserUtil and returns the display name:

something like getDisplayName(String user) { return UserUtil.getUserByName(user).getDisplayName()}

and then call it in velocity:

$displayName("bla")

This widget could not be displayed.

Thanks for your answer,

I didn't do exactly what you wrote but that helped me to understand the existing code I'm working on. I add the method getDisplayName to the utilities class and then, I put an instance of this class in the context map so that I can use it in the velocity template.

Here's my code for people having the same issue:

Utility class:

import com.atlassian.jira.user.UserUtils;
...
 public String getDisplayName(String username) {
        return UserUtils.getUser(username).getDisplayName();
    }
...

Java class of the plugin:

Utilities utils = new Utilities();
...
contextMap.add("utils", utils);

Velocity template:

$utils.getDisplayName("some username")

Now, it works!

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

216 views 3 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you