Show last authors
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
25 1.1 Add objects to a page
26
27 Here a piece of code to show how is possible to store a new object in one page:
28
29 {code}
30 ## Create an object
31 #set($obj = $doc.newObject("XWiki.SomeClass"))
32 $obj.set("field1",$value1)
33 $obj.set("field2",$value2)
34
35 ## Save the object in the page
36 $doc.save()
37 {code}
38
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.
41
42 1.1 Access objects in a page
43
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 :
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
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
51 #set($field1 = $obj.get("field1"))
52 SomeSpace.SomeClass[0] : field1 = "$field1"
53
54 {code}
55
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
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].
69
70 1.1 Include a Velocity page into another Velocity page
71
72 See [Code.IncludeInVelocity].

Get Connected