Best velocity practice querying page (class) property

Xwiki version: 16.3.1

Good afternoon everyone!

I think I have a pretty straightforward ‘challenge’ that I’ve spend too long trying to solve, I’m trying to retrieve a (class) property of another page.

I have the full adress of this other page: in the example case it’s : "Main.Werkwijze.Formulieren.Rapport van oplevering BMI"

When viewing the object of the page the class is listed as :

Main.Werkwijze.Formulieren.Code.FormulierenClass

when looking at the properties of the class object I can see the values I want to retrieve

So I’ve attempted the following so far (none return any value):

#set ($xdoc=$xwiki.getDocument($Formulier))
#set ($xobject = $xdoc.getObject('Main.Werkwijze.Formulieren.Code.FormulierenClass'))
#set ($xclass = $xobject.xWikiClass)
#foreach ($property in $xclass.properties)
  $property.Name
  $property.value
#end
#set ($xdoc=$xwiki.getDocument($Formulier))
$xdoc.display('Versienummer')
$xdoc.getValue('Versienummer')

I’ve also attempted variations of the class reference #set ($xobject = $xdoc.getObject('Main.Werkwijze.Formulieren.Code.FormulierenClass')) as it seems the $xobject variable remains empty.

Now I know from the documentation that there are $service queries that can be run on all the objects of a class aswell, and Im fairly confident that I can get my solution through that route… but I find myself wondering what the best practice is here… and perhaps a hint as to where I’m going wrong.

Thank you in advance for any and all feedback!

Greetings from the NL!

Are you sure $xdoc and $Formulier are defined and not empty?

Hey @acotiuga , what I did to check was:

#set($Formulier=$doc.getValue('Formulier'))
#set ($xdoc=$xwiki.getDocument($Formulier))
$Formulier
$xdoc

this output:

[Main.Werkwijze.Formulieren.Rapport van oplevering BMI]
"Main.Werkwijze.Formulieren.Rapport van oplevering BMI"

I think my issue is in the second part:

++++
#set ($xobject = $xdoc.getObject('Main.Werkwijze.Formulieren.Code.FormulierenClass'))
#set ($xclass = $xobject.xWikiClass)
$xobject
$xclass

this output:

++++
$xobject
$xclass

And if you check the page in object mode, the object is there, right?

Indeed!

The OP screenshot of the object view was / is from the [Main.Werkwijze.Formulieren.Rapport van oplevering BMI]
“Main.Werkwijze.Formulieren.Rapport van oplevering BMI”

page

Here I can see the Main.Werkwijze.Formulieren.Code.FormulierenClass reference also, hence my confusion.

To add to my mystery I’m sure the below code ran fine before the update to 16.3

#set($Formulier=$doc.getValue('Formulier'))
#set ($xdoc=$xwiki.getDocument($Formulier))
$xdoc.getValue('Formuliernummer')

Hence why i started this post, I figured perhaps the using of getDocument / getValue was deprecated and we had to use service queries now.

Short update.

Another page with the exact same code is working normally

#set($Formulierdoc=$xwiki.getDocument($Formulier))
|=Formuliernummer|$Formulierdoc.getValue('Formuliernummer')
|=Versienummer (Huidig)|$Formulierdoc.display('Versienummer')

Outputs:

Formuliernummer	78
Versienummer (Huidig)	0.5

The strange thing is that the input is the same, and the ‘other page’ is of the same class / setup (including object view) also… I’m stumped!

$Formulier
$Formulierdoc

outputs:
[Main.Werkwijze.Formulieren.Declaratieformulier]
Main.Werkwijze.Formulieren.Declaratieformulier

Great, then you have your answer: the code works as expected. You have to compare now the 2 pages to find the breakage.

1 Like

Hi, I am your debugger for today…

#set ($xdoc=$xwiki.getDocument($Formulier))
#set ($xobject = $xdoc.getObject('Main.Werkwijze.Formulieren.Code.FormulierenClass'))
#set ($xclass = $xobject.xWikiClass)
#foreach ($property in $xclass.properties)
  $xobject.getProperty($property.name).value
#end

Voila :slight_smile:

Hey Jan-Paul!

Appreciate the assist, unfortunately I think its not the code but something in the pages…

whichever way I look at it though they appear the same… I suspect it is a rights issue of sorts …

i.e. if I adjust your code to be:

 #set ($xdoc=$xwiki.getDocument($Formulier))
xdoc: $xdoc
#set ($xobject = $xdoc.getObject('Main.Werkwijze.Formulieren.Code.FormulierenClass'))
xobject : $xobject
#set ($xclass = $xobject.xWikiClass)
xclass: $xclass 
#foreach ($property in $xclass.properties)
  $xobject.getProperty($property.name).value
#end

it outputs:

xdoc: "Main.Werkwijze.Formulieren.Onderhoudsplan OAI" 
xobject : $xobject
xclass: $xclass 

So it appears to me the formulierenclass is not being loaded… however the url for the formulierenclass is /Main/Werkwijze/Formulieren/Code/FormulierenClass and when I test with a link to [[test>>doc:Main.Werkwijze.Formulieren.Code.FormulierenClass]] it sends me to the Classpage just fine

I checked rights, objects on the pages etc. but cannot find the issue… but Not giving up just yet!

This means that there’s no xobject Main.Werkwijze.Formulieren.Code.FormulierenClass in the Main.Werkwijze.Formulieren.Onderhoudsplan OAI page (i.e. getObject() returns null).

Hey @vmassol , thanks for the response during your busy day!

I understand that it’s not retrieving the object, I’m just unsure why it isnt now (and why it was before / is working on some pages!)

Also a sidenote it’s supposed to get an attribute of a page from another class, not of the current page!

Basically the setup is that there is an class that describes forms and a class that logs changes to the forms ; When making a ‘logging’ page the user selects a form page, and the script is supposed to retrieve data of that form page.

The script I was using was:

Formulier : $Formulier
#set($Formulierdoc=$xwiki.getDocument($Formulier))
Formulierdoc : $Formulierdoc
$Formulierdoc.getValue('Formuliernummer')

On the pages where this script runs sucesfully I get:

Formulier : [Main.Werkwijze.Formulieren.Declaratieformulier]
Formulierdoc : Main.Werkwijze.Formulieren.Declaratieformulier
78

However on most of the newer pages it outputs:

Formulier : [Main.Werkwijze.Formulieren.Rapport van oplevering BMI]
Formulierdoc : "Main.Werkwijze.Formulieren.Rapport van oplevering BMI"
$Formulierdoc.getValue('Formuliernummer')

Note that the page absolutely exists and the [Main.Werkwijze.Formulieren.Rapport van oplevering BMI] link can be followed to the page normally

So I suppose the question comes down to why the script is outputting “” for the one document and the name without the " in the other