This guide covers the main XWiki APIs. It's not meant to be comprehensive. For that you'll need to check the XWiki Reference API page.

Querying documents

See the HQL Velocity Examples page.

Getting external content

You can use the following APIs to get content located at external URLs:

public String getURLContent(String surl, String username, String password) throws IOException
public String getURLContent(String surl) throws IOException
public String getURLContent(String surl, String username, String password, int timeout) throws IOException
public String getURLContent(String surl, int timeout) throws IOException
public byte[] getURLContentAsBytes(String surl, String username, String password)
public byte[] getURLContentAsBytes(String surl) throws IOException

For example:

$xwiki.getURLContent("http://google.com")

Add objects to a page

Here a piece of code to show how is possible to store a new object in one page:

## Create an object
#set($obj = $doc.newObject("XWiki.SomeClass"))
$obj.set("field1",$value1)
$obj.set("field2",$value2)

## Save the object in the page
$doc.save()

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.

Access objects in a page

Here is a piece of code to show how it is possible to access an object attached to the page, and read its fields :

## Retrieve the first object (index [0]) among all objects attached to this page and of a certain class
#set($obj = $doc.getObject("SomeSpace.SomeClass"))

## Retrieve the value of the propertty "field1" for this object, provided a property called "field1" is actually defined in the class of this object
#set($field1 = $obj.get("field1"))
SomeSpace.SomeClass[0] : field1 = "$field1"

You can also go through all properties of an object, without knowing their name respective names. That's how the default Class Sheet works, when you create a class using the Class Wizard.

#set($class = $obj.xWikiClass) ## access the class object representing SomeSpace.SomeClass
 #foreach($prop in $class.properties) ## go through all properties
<dt> *${prop.prettyName}* </dt>
<dd>$doc.display($prop.getName())</dd>
#end

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 you want to include somewhere else, and uses the display(propertyName) method to access properties of an object attached to the including page, you have to use the includeForm() Velocity macro in the including script :

#includeForm("spacename.docname")

See the reference for the includeForm() macro.

Include a Velocity page into another Velocity page

See Include In Velocity.

Redirecting to another page

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:

$response.sendRedirect($xwiki.getURL("Space.Page"))
Tags:
Created by Vincent Massol on 2007/10/03
   

Get Connected