Wiki source code of APIGuide

Version 4.1 by Silvia Macovei on 2010/02/25

Hide last authors
Silvia Macovei 4.1 1 = XWiki API Guide =
Vincent Massol 1.1 2
Silvia Macovei 4.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
Silvia Macovei 4.1 5 {{toc start="" depth="" numbered=""/}}
Vincent Massol 1.1 6
Silvia Macovei 4.1 7 == Querying documents ==
Vincent Massol 2.2 8
Silvia Macovei 4.1 9 See the [[HQL Velocity Examples page>>velocityHqlExamples]].
Vincent Massol 2.2 10
Silvia Macovei 4.1 11 == Getting external content ==
Vincent Massol 1.1 12
13 You can use the following APIs to get content located at external URLs:
14
Silvia Macovei 4.1 15 {{code language="none"}}
Vincent Massol 1.1 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
Silvia Macovei 4.1 22 {{/code}}
Vincent Massol 1.1 23
24 For example:
25
Silvia Macovei 4.1 26 {{code language="none"}}$xwiki.getURLContent("http://google.com"){{/code}}
Vincent Massol 1.1 27
Silvia Macovei 4.1 28 == Add objects to a page ==
29
VitantonioMessa 1.2 30 Here a piece of code to show how is possible to store a new object in one page:
31
Silvia Macovei 4.1 32 {{code}}
VitantonioMessa 1.8 33 ## Create an object
VitantonioMessa 1.6 34 #set($obj = $doc.newObject("XWiki.SomeClass"))
35 $obj.set("field1",$value1)
36 $obj.set("field2",$value2)
VitantonioMessa 1.8 37
38 ## Save the object in the page
VitantonioMessa 1.4 39 $doc.save()
Silvia Macovei 4.1 40 {{/code}}
VitantonioMessa 1.2 41
Silvia Macovei 4.1 42 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.
jeanvivienmaurice 1.13 43
Silvia Macovei 4.1 44 == Access objects in a page ==
jeanvivienmaurice 1.13 45
jeanvivienmaurice 1.14 46 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 47
Silvia Macovei 4.1 48 {{code}}
jeanvivienmaurice 1.13 49 ## Retrieve the first object (index [0]) among all objects attached to this page and of a certain class
50 #set($obj = $doc.getObject("SomeSpace.SomeClass"))
51
jeanvivienmaurice 1.14 52 ## 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 53 #set($field1 = $obj.get("field1"))
54 SomeSpace.SomeClass[0] : field1 = "$field1"
Silvia Macovei 4.1 55 {{/code}}
jeanvivienmaurice 1.13 56
jeanvivienmaurice 1.14 57 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.
58
Silvia Macovei 4.1 59 {{code}}
60 #set($class = $obj.xWikiClass) ## access the class object representing SomeSpace.SomeClass
jeanvivienmaurice 1.14 61 #foreach($prop in $class.properties) ## go through all properties
62 <dt> *${prop.prettyName}* </dt>
63 <dd>$doc.display($prop.getName())</dd>
64 #end
Silvia Macovei 4.1 65 {{/code}}
jeanvivienmaurice 1.14 66
Silvia Macovei 4.1 67 Actually the line
jeanvivienmaurice 1.20 68
Silvia Macovei 4.1 69 {{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 {{html clean="false" wiki="true"}}<code>display(propertyName)</code> method to access properties of an object attached to the including page, you have to use the <code>includeForm()</code>{{/html}} Velocity macro in the including script :
jeanvivienmaurice 1.21 70
Silvia Macovei 4.1 71 {{code}}#includeForm("spacename.docname"){{/code}}
Vincent Massol 3.1 72
Silvia Macovei 4.1 73 See [[the reference for the <code>includeForm()</code> macro>>code:Macros.IncludeFormMacro]].
Vincent Massol 3.1 74
Silvia Macovei 4.1 75 == Include a Velocity page into another Velocity page ==
76
77 See [[DevGuide.IncludeInVelocity]].
78
79 == Redirecting to another page ==
80
Vincent Massol 3.1 81 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.
82
83 Example:
84
Silvia Macovei 4.1 85 {{code}}$response.sendRedirect($xwiki.getURL("Space.Page")){{/code}}

Get Connected