General Actions:
This guide covers the main XWiki APIs that you can use from scripts in wiki pages. It's not meant to be comprehensive. For that you'll need to check the XWiki Reference API page.
Note that while most examples are written in Velocity you can use any other scripting language to access the same APIs.
See the Query Module for examples on how to perform queries on the wiki using a scripting language.
For example in Velocity:
You can access the HTTP Request by accessing the com.xpn.xwiki.web.XWikiServletRequest object through the request script variable.
For example in Velocity, to access an action HTTP parameter passed in the request you would write:
Note that this is a shortcut to:
You can use the following APIs to get content located at external URLs:
For example in Velocity:
Here is a piece of Velocity script to show how is possible to store a new object in one page:
The "XWiki.SomeClass" class has to be created (through the class editor): field1 and field2 are property of the class. At the moment, this code works fine only if the user currently logged in has editing rights on the page, otherwise the Document.save() will not work.
Here is a piece of Velocity script to show how it is possible to access an object attached to the page, and read its fields :
You can also go through all the properties of an object, without knowing their respective names. That's how the default Class Sheet works, when you create a class using the Class Wizard.
Actually the line $doc.display(propertyName) can either display the property value, or generate an input field in the page, mapped to the property whose name is passed as argument (when you edit the page in inline mode). If you have a Velocity script that uses the display(propertyName) method to access properties of an object attached to the including page and you want to include it somewhere else you have to use the includeForm() Velocity macro in the including script:
See the includeForm() macro for more information.
See the Include In Velocity tutorial.
It's possible to redirect the user to another page. This is useful for example when a page has been removed and you have given the URL to someone so you want the old page to redirect to the new page.
Example:
For more examples, see the Redirect Snippet.
For example, in Velocity: