Are setters implemented in user scripting API?

Do these work in velocity? I cant make it work. It doesnt show any error but also doesnt do anything.

#set ($discard = $‘NewName’))
#set ($discard = $
#set ($discard = $

Hi, the error is that you’re calling getProperties() at each line and this returns a new object every time.

So you need to write:

#set ($props = $
#set ($discard = $props.setFirstName('David1'))
#set ($discard = $props.setDisplayHiddenDocuments(false))
#set ($discard = $

I need to check if I can modify the API a bit to support the way you wrote it since I think it would make the API simpler to use and would be more logical.

Now I’ve tried your example and the setDisplayHiddenDocuments is failing. That’s a bug. I’ve raised Loading...


1 Like

Note that this user API is recent and still marked as @Unstable which explains a bit the reason why you found these problems. Good feedback to improve it, thanks! :wink:

Note that without this API the old way to do it was getting the XWiki.XWikiUsers xobject directly and setting properties in it.

Thank you. I was using the old ways directly from the document (XWiki.XWikiUsers) however the result I got was translated value (so I had to do hardcoded translation in the code to get the boolean value)

#set($reference = $xcontext.getUserReference())
#set($document = $xwiki.getDocument($reference))
#set($object = $document.getObject('XWiki.XWikiUsers'))
#set($displayHiddenDocuments = $object.displayHiddenDocuments)

$displayHiddenDocuments returns ‘Yes’ or ‘Si’ or ‘Ano’ according to the language of the subwiki.

Now fixed in Loading...

To set an xproperty value you need to use set as in (for ex).


$object('displayHiddenDocuments', '0')

When you write this, you’re actually calling $object.get('displayHiddenDocuments') (you’re using a Velocity shorthand) and the get()API is a misnamer it does a display. See!%2Fcom%2Fxpn%2Fxwiki%2Fapi%2FObject.html%23get-java.lang.String-

1 Like