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 = $services.user.properties.setFirstName(‘NewName’))
#set ($discard = $services.user.properties.setDisplayHiddenDocuments(false))
#set ($discard = $services.user.properties.save())

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:

{{velocity}}
#set ($props = $services.user.properties)
#set ($discard = $props.setFirstName('David1'))
#set ($discard = $props.setDisplayHiddenDocuments(false))
#set ($discard = $props.save())
{{/velocity}}

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...

Thanks

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 https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Scripting/APIGuide/#HAddobjectstoapage (for ex).

So:

$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 https://www.xwiki.org/xwiki/bin/view/ScriptingDocumentation/?url=http:%2F%2Fnexus.xwiki.org%2Fnexus%2Fservice%2Flocal%2Frepositories%2Fpublic%2Farchive%2Forg%2Fxwiki%2Fplatform%2Fxwiki-platform-oldcore%2F12.6.4%2Fxwiki-platform-oldcore-12.6.4-javadoc.jar%2F!%2Fcom%2Fxpn%2Fxwiki%2Fapi%2FObject.html%23get-java.lang.String-

1 Like