It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

NEED example of a user macro that retrieves the current username from the context

I see many of snippets and sample expressions tossed around on this site, but no single coherent example for a non-programmer like myself.

I just want a user macro that will return the current username (not the full name). I want to feed this in to an sql query that will pull information about this user out of a local database.

Come on, easy points for some advanced technician out there! Thanks, R.M.

P.S. Extra points (unofficially) for pointers to some documentation that would show me how to do this myself.

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

7 votes
Answer accepted
Here's a user macro called {my-username} it should be self evident I hope
## Macro title: my-username
## Macro has a body: N
## Body processing: Selected body processing option
## Output: Selected output option
## Developed by: My Name
## Date created: dd/mm/yyyy
## Installed by: My Name

Hi, Can i get sample code for displaying current date? I've tried $getDateFormat() and other stuff but it doesnt work! May be im missing something....



To find out which Java objects are exposed in velocity and thus help you with writing your own user macro, I'd recommend:

Thanks, David, this help a lot.

{tip:title=this works for me}

Current user fullname: {user-info:fullname}

Current userid: {user-info:username}

You can pass it to a variable in groovy:
mport com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.util.GeneralUtil;
import com.atlassian.spring.container.ContainerManager;
import com.atlassian.confluence.pages.Attachment;
import com.atlassian.confluence.pages.AttachmentManager;
import com.atlassian.confluence.util.velocity.VelocityUtils;
import com.atlassian.confluence.web.context.StaticHttpContext;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import javax.activation.MimetypesFileTypeMap;
import java.util.Date;
def String username = '{user-info:username}'
println("current user : " + username + "\n");

Thanks, Anh. This did the trick.

Yes it did! just add another parameter, as in "{user-info:email|user=@self}"

No it didn't! This returns information about the user that created the page, not the current user. Is there another example that would work?

For clarity, this line: def String username = '{user-info:username}' Did not establish the username when groovy ran that line, instead it simply embedded the macro expression verbatim in its stdout. That stdout, when when passed to Confluence, gets treated as content on the page, and Confluence expands it. Or, am I wrong?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

Community showcase
Published in Confluence

2019: All Wrap(ped) Up Like a Present

Hindsight might be 2020 but looking back, a lot of cool new features rolled out in 2019. From new collaboration and organizational powers to fresh templates and handy integrations, it’s been quite th...

32,884 views 64 66
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you