Wiki source code of APIGuide

Version 3.2 by Vincent Massol on 2009/09/08

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
Vincent Massol 2.2 7 1.1 Querying documents
8
9 See the [HQL Velocity Examples page>velocityHqlExamples].
10
Vincent Massol 1.1 11 1.1 Getting external content
12
13 You can use the following APIs to get content located at external URLs:
14
15 {code:none}
16 public String getURLContent(String surl, String username, String password) throws IOException
17 public String getURLContent(String surl) throws IOException
18 public String getURLContent(String surl, String username, String password, int timeout) throws IOException
19 public String getURLContent(String surl, int timeout) throws IOException
20 public byte[] getURLContentAsBytes(String surl, String username, String password)
21 public byte[] getURLContentAsBytes(String surl) throws IOException
22 {code}
23
24 For example:
25 {code:none}
Vincent Massol 3.2 26 $xwiki.getURLContent("http://google.com")
Vincent Massol 1.1 27 {code}
28
VitantonioMessa 1.2 29 1.1 Add objects to a page
Vincent Massol 1.1 30
VitantonioMessa 1.2 31 Here a piece of code to show how is possible to store a new object in one page:
32
33 {code}
VitantonioMessa 1.8 34 ## Create an object
VitantonioMessa 1.6 35 #set($obj = $doc.newObject("XWiki.SomeClass"))
36 $obj.set("field1",$value1)
37 $obj.set("field2",$value2)
VitantonioMessa 1.8 38
39 ## Save the object in the page
VitantonioMessa 1.4 40 $doc.save()
VitantonioMessa 1.2 41 {code}
42
43 The "XWiki.SomeClass" class has to be created (through the class editor): field1 and field2 are property of the class.
VitantonioMessa 1.12 44 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 45
jeanvivienmaurice 1.14 46 1.1 Access objects in a page
jeanvivienmaurice 1.13 47
jeanvivienmaurice 1.14 48 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 49
50 {code}
51 ## Retrieve the first object (index [0]) among all objects attached to this page and of a certain class
52 #set($obj = $doc.getObject("SomeSpace.SomeClass"))
53
jeanvivienmaurice 1.14 54 ## 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 55 #set($field1 = $obj.get("field1"))
56 SomeSpace.SomeClass[0] : field1 = "$field1"
57
58 {code}
59
jeanvivienmaurice 1.14 60 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.
61
62 {code}
63 #set($class = $obj.xWikiClass) ## access the class object representing SomeSpace.SomeClass
64 #foreach($prop in $class.properties) ## go through all properties
65 <dt> *${prop.prettyName}* </dt>
66 <dd>$doc.display($prop.getName())</dd>
67 #end
68 {code}
69
jeanvivienmaurice 1.20 70 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 71 {code}#includeForm("spacename.docname"){code}
Vincent Massol 1.22 72 See [the reference for the <code>includeForm()</code> macro|code:Macros.IncludeFormMacro].
jeanvivienmaurice 1.20 73
jeanvivienmaurice 1.21 74 1.1 Include a Velocity page into another Velocity page
75
Vincent Massol 2.1 76 See [DevGuide.IncludeInVelocity].
Vincent Massol 3.1 77
78 1.1 Redirecting to another page
79
80 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.
81
82 Example:
83
84 {code}
85 $response.sendRedirect($xwiki.getURL("Space.Page"))
86 {code}
87

Get Connected