User mention Macro - REST API
Asked by Orlando_Jurcone:

Hi,

I am trying to create a Confluence Page using REST API and mention some existing users. Do do this I am using this macro:

\<ac:link\>  
\<ri:user ri:userkey="123456789"/\>  
\</ac:link\>

It works perfectly, but I can only get my userkey. To get others I need to ask each one particularly or query database for which I don't have access.

I tried something like this:

\<ac:link\>  
\<ri:user ri:user="user"/\>  
\</ac:link\>

but I got:

{"statusCode":500,"data":{"authorized":false,"valid":true,"errors":\[\],"successful":false},"message":"java.lang.RuntimeException: com.atlassian.confluence.content.render.xhtml.XhtmlException: com.atlassian.confluence.content.render.xhtml.XhtmlException: Missing required attribute: {<http://atlassian.com/resource/identifier}username>"}  
500

Is there a way to mention a Confluence user using Rest API with only the user name (not user-key)?

Thanks.


Answers:
author: Bill Bailey
body: You are writing a user macro? If so you can try this code:

```
<p><ac:link>
#set($userInfo = $userAccessor.getUserByName("$userEntry")) 
<ri:user ri:userkey="$userInfo.key"/>
</ac:link></p>
```

Where $userEntry is the user name you want is selected using the [@Param](/t5/user/viewprofilepage/user-id/1345631) XXXX\|type=username

comments: author: Orlando_Jurcone
body: Hi Bill,

Thanks for the quick response.

Unfortunately I don't have System Administrator permissions for creating new macros. I am curios if there is one already available like this one that requires the userkey.


author: Bill Bailey
body: I am not aware of any. I guess it is time to bribe your admin to install user macros for you. ;-)

You could set up a local copy of Confluence where you can be an admin and do all your testing, so that your admin would just have to install the macro once it is done.


author: Jineesh M G
body: Hi Bill,

I am able to do the same thing but once I update the comment/description with this macro, no notification received by the mentioned user.

But if I use @ symbol along with the user notification email working fine. Any idea?


author: Bill Bailey
body: I suspect that the mention function is driven by a client-side script versus the server. In other words, the user macro will display a user name in a similar fashion, it won't trigger the mention script/autocomplete, driven by the shortcut the @ key.

However, all it not lost (maybe), as there is a user mention macro, so you could just use the storage format inside your user macro, and it *should* work.


author: Jineesh M G
body: Hi Bill,

How can I get the storage format? I got the source code as below:

\<ac:link\>  
\<ri:user ri:userkey="xxxxxxx" /\>  
\</ac:link\>

I have implemented this but it just shown the User. No notification.

Could you please help?


author: M?rten Woxberg
body: Thanks [@Bill Bailey](/t5/user/viewprofilepage/user-id/502089)

Thought we where loosing our minds searching for the answer to this.


author: Radek Janata
body: Hi Orlando,

it's sufficient to use 'username' instead of 'userkey'. Confluence will transform it automatically from 'username' to 'userkey'.

```
<ac:link>
<ri:user ri:username="name.surname"/>
</ac:link>
```

Regards,  
Radek


Tags: rest-api