Wiki source code of APIGuide

Version 1.22 by Vincent Massol on 2007/12/10

Hide last authors
Vincent Massol 1.1 1 1 XWiki API Guide
2
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].
4
5 #toc("" "" "")
6
7 1.1 Getting external content
8
9 You can use the following APIs to get content located at external URLs:
10
11 {code:none}
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
18 {code}
19
20 For example:
21 {code:none}
22 $xwiki.getURLContent("http://teamcity.xwiki.org/externalStatus.html")
23 {code}
24
VitantonioMessa 1.2 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
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()
VitantonioMessa 1.2 37 {code}
38
39 The "XWiki.SomeClass" class has to be created (through the class editor): field1 and field2 are property of the class.
VitantonioMessa 1.12 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
jeanvivienmaurice 1.14 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
46 {code}
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
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
58 {code}
59 #set($class = $obj.xWikiClass) ## access the class object representing SomeSpace.SomeClass
60 #foreach($prop in $class.properties) ## go through all properties
61 <dt> *${prop.prettyName}* </dt>
62 <dd>$doc.display($prop.getName())</dd>
63 #end
64 {code}
65
jeanvivienmaurice 1.20 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 :
jeanvivienmaurice 1.19 67 {code}#includeForm("spacename.docname"){code}
Vincent Massol 1.22 68 See [the reference for the <code>includeForm()</code> macro|code:Macros.IncludeFormMacro].
jeanvivienmaurice 1.20 69
jeanvivienmaurice 1.21 70 1.1 Include a Velocity page into another Velocity page
71
72 See [Code.IncludeInVelocity].

Get Connected