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
Accepted answer
David Simpson Community Champion 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 Champion 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
Community showcase

Scrum Roles Explained: the Do's and the Don'ts

Hello Community,  Today we are going to talk about the three Scrum Roles. There is the Development Team, the Scrum Master and the Product Owner. In my opinion these three are all really impo...

126 views 2 5
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