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

7 votes
Answer accepted
David Simpson Community Leader Sep 22, 2011
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
$action.remoteUser.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....

Thanks,

Avinash

2 votes
David Simpson Community Leader Sep 22, 2011

To find out which Java objects are exposed in velocity and thus help you with writing your own user macro, I'd recommend:
https://developer.atlassian.com/display/CONFDEV/Confluence+Objects+Accessible+From+Velocity

Thanks, David, this help a lot.

{tip:title=this works for me}

Current user fullname: {user-info:fullname}

Current userid: {user-info:username}

{tip}
You can pass it to a variable in groovy:
{groovy:output=wiki}
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 org.apache.poi.ss.usermodel.*;
import javax.activation.MimetypesFileTypeMap;
import java.util.Date;
def String username = '{user-info:username}'
println("current user : " + username + "\n");
{groovy}

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?

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published Thursday in Confluence

Confluence CVEs and common questions

Two vulnerabilities have been published for Confluence Server and Data Center recently: March 20, 2019 CVE-2019-3395 / CVE-2019-3396 April 17, 2019 CVE-2019-3398 The goal of this article is...

116 views 0 10
Read article

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