Velocity Question - document.getContent() - empty?

Hi,

I have a Velocity script direct in a page from where I want to do some scripting and access other pages. The script is executing and the Document Object doc is not empty. All the other outputs are working. But only getContent is always empty.
Any ideas?

   #set ($doc = $xwiki.getDocument("MySpace.MyPage"))
    <p>Document $page Syntax $doc.getSyntax() Space $doc.getSpace() Reference $doc.getDocumentReference()</p>
    #set ($content = $doc.getContent())
    <p>Dom:  $doc.getXDOM()</p>
    <p>Content: $content</p>

thx
Joesph

Hello, first you should not define a $doc variable since it’s already defined and a reserved name.

Then in your script you use a $page variable but it’s not defined.

IMO your reference is wrong but since you’re not showing what you’ve used, it’s hard to help you more. Just navigate to your page and click on the Information tab to get the page reference.

PS: This is not a question for the “Development” category (please read its description). I’ve moved it to “Help/Discuss”. Thx

Sorry for the wrong post.
Thx for your quick reply.

I am a velocity and XWiki API beginner.

I have just posted a snipplet, the page variable is defined in a loop outside…
The doc variable is now renamed in xdoc, but that was not the reason.

It was indeed a wrong page reference!

The story is, the script is for a migration of pages to restore a page hierachy from another Wiki (TWiki). All pages are already imported in Xwiki and with the “Nested Pages Migration” converted from terminal pages to nested pages.
But the structure is flat at the moment.
In the migrated flat documents there is a meta data field somewhere in a comment which contains the old parent. The script should iterate over all flat documents, extract the ParentDocument out of the metadata and restore the hierarchy again.
`
Is there a good API-DOC.
I have already looked in the Code :wink:

At the moment I am struggling with setting a parent page. How is this done?

$xdoc.setParent($parent)
$xdoc.save()

The concept of parent is an old concept (prior to nested pages), that’s been deprecated, see https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/ContentOrganization/NestedPagesMigration/#HHistory

You just need to create your page using the right reference to be in a hierarchy, for example A.B.WebHome (using a document reference, or A.B using a page reference), means that the B page is under the A page.

See https://extensions.xwiki.org/xwiki/bin/view/Extension/Model%20Module to understand how references work.

Hope it helps

Thanks a lot for the links.
I have installed the ScriptAPI-Reference now into my xwiki.

All methods only take one argument for the document/page.

$services.model.createDocumentReference()...
$services.model.createPageReference()...

Only the entity reference has the possibility to set a parent entity reference.

#set ($entityTypeClass = $xwiki.classLoader.loadClass('org.xwiki.model.EntityType'))
#set ($documentEntityType = $entityTypeClass.DOCUMENT)
#set ($parentEntityRef = $services.model.createEntityReference($parent, $documentEntityType))
#set ($entityRef = $services.model.createEntityReference( $page, $documentEntityType, $parentEntityRef))

But xwiki here reports “Failed to execute the [velocity] macro. Cause: [An Entity Reference type cannot be null]. Click on this message for details.”

But I did not get it … how to create hierarchical a link between two documents/pages in the velocity script?

Hi,

the solution for me was, to set the parent pages in the old XWiki 7.3 version with Velocity and then transfer the Pages to current 15.10. LTS Version. In the current version use the “Nested Pages Migration” Extension. It has done the job to convert the terminal pages into nested pages and restore the whole deep hierarchy respecting the structure.

[https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/ContentOrganization/NestedPagesMigration/](https://Migration Guide to Nested Pages)

#set ($childDoc = $xwiki.getDocument($page))
$childDoc.setParent($parent)
$childDoc.save()