Wiki source code of APIGuide

Version 7.1 by Jerome on 2010/12/23

Hide last authors
Jerome 7.1 1 1 XWiki API Guide
Vincent Massol 1.1 2
Jerome 7.1 3 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>API].
Vincent Massol 1.1 4
Jerome 7.1 5 #toc("" "" "")
Vincent Massol 2.2 6
Jerome 7.1 7 1.1 Getting external content
Vincent Massol 2.2 8
Vincent Massol 1.1 9 You can use the following APIs to get content located at external URLs:
10
Jerome 7.1 11 {code:none}
Vincent Massol 1.1 12 public String getURLContent(String surl, String username, String password) throws IOException
13 public String getURLContent(String surl) throws IOException
14 public String getURLContent(String surl, String username, String password, int timeout) throws IOException
15 public String getURLContent(String surl, int timeout) throws IOException
16 public byte[] getURLContentAsBytes(String surl, String username, String password)
17 public byte[] getURLContentAsBytes(String surl) throws IOException
Jerome 7.1 18 {code}
Vincent Massol 1.1 19
20 For example:
Jerome 7.1 21 {code:none}
22 $xwiki.getURLContent("http://teamcity.xwiki.org/externalStatus.html")
23 {code}
Vincent Massol 1.1 24
Jerome 7.1 25 1.1 Add objects to a page
Vincent Massol 1.1 26
VitantonioMessa 1.2 27 Here a piece of code to show how is possible to store a new object in one page:
28
Jerome 7.1 29 {code}
VitantonioMessa 1.8 30 ## Create an object
VitantonioMessa 1.6 31 #set($obj = $doc.newObject("XWiki.SomeClass"))
32 $obj.set("field1",$value1)
33 $obj.set("field2",$value2)
VitantonioMessa 1.8 34
35 ## Save the object in the page
VitantonioMessa 1.4 36 $doc.save()
Jerome 7.1 37 {code}
VitantonioMessa 1.2 38
Jerome 7.1 39 The "XWiki.SomeClass" class has to be created (through the class editor): field1 and field2 are property of the class.
40 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.
jeanvivienmaurice 1.13 41
Jerome 7.1 42 1.1 Access objects in a page
jeanvivienmaurice 1.13 43
jeanvivienmaurice 1.14 44 Here is a piece of code to show how it is possible to access an object attached to the page, and read its fields :
jeanvivienmaurice 1.13 45
Jerome 7.1 46 {code}
jeanvivienmaurice 1.13 47 ## Retrieve the first object (index [0]) among all objects attached to this page and of a certain class
48 #set($obj = $doc.getObject("SomeSpace.SomeClass"))
49
jeanvivienmaurice 1.14 50 ## Retrieve the value of the propertty "field1" for this object, provided a property called "field1" is actually defined in the class of this object
jeanvivienmaurice 1.13 51 #set($field1 = $obj.get("field1"))
52 SomeSpace.SomeClass[0] : field1 = "$field1"
53
Jerome 7.1 54 {code}
55
jeanvivienmaurice 1.14 56 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.
57
Jerome 7.1 58 {code}
59 #set($class = $obj.xWikiClass) ## access the class object representing SomeSpace.SomeClass
jeanvivienmaurice 1.14 60 #foreach($prop in $class.properties) ## go through all properties
61 <dt> *${prop.prettyName}* </dt>
62 <dd>$doc.display($prop.getName())</dd>
63 #end
Jerome 7.1 64 {code}
jeanvivienmaurice 1.14 65
Jerome 7.1 66 Actually the line {code}$doc.display(propertyName){code} 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 <code>display(propertyName)</code> method to access properties of an object attached to the including page, you have to use the <code>includeForm()</code> Velocity macro in the including script :
67 {code}#includeForm("spacename.docname"){code}
68 See [the reference for the <code>includeForm()</code> macro|extensions:Extension.Include Form Macro].
jeanvivienmaurice 1.20 69
Jerome 7.1 70 1.1 Include a Velocity page into another Velocity page
Vincent Massol 3.1 71
Jerome 7.1 72 See [Code.IncludeInVelocity].

Get Connected