Wiki source code of XWiki API Guide

Version 5.1 by Silvia Macovei on 2010/02/25

Hide last authors
Silvia Macovei 4.1 1 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 2
Silvia Macovei 5.1 3 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
Vincent Massol 1.1 4
Silvia Macovei 5.1 5 = Querying documents =
Vincent Massol 2.2 6
Silvia Macovei 4.1 7 See the [[HQL Velocity Examples page>>velocityHqlExamples]].
Vincent Massol 2.2 8
Silvia Macovei 5.1 9 = Getting external content =
Vincent Massol 1.1 10
11 You can use the following APIs to get content located at external URLs:
12
Silvia Macovei 4.1 13 {{code language="none"}}
Vincent Massol 1.1 14 public String getURLContent(String surl, String username, String password) throws IOException
15 public String getURLContent(String surl) throws IOException
16 public String getURLContent(String surl, String username, String password, int timeout) throws IOException
17 public String getURLContent(String surl, int timeout) throws IOException
18 public byte[] getURLContentAsBytes(String surl, String username, String password)
19 public byte[] getURLContentAsBytes(String surl) throws IOException
Silvia Macovei 4.1 20 {{/code}}
Vincent Massol 1.1 21
22 For example:
23
Silvia Macovei 4.1 24 {{code language="none"}}$xwiki.getURLContent("http://google.com"){{/code}}
Vincent Massol 1.1 25
Silvia Macovei 5.1 26 = Add objects to a page =
Silvia Macovei 4.1 27
VitantonioMessa 1.2 28 Here a piece of code to show how is possible to store a new object in one page:
29
Silvia Macovei 4.1 30 {{code}}
VitantonioMessa 1.8 31 ## Create an object
VitantonioMessa 1.6 32 #set($obj = $doc.newObject("XWiki.SomeClass"))
33 $obj.set("field1",$value1)
34 $obj.set("field2",$value2)
VitantonioMessa 1.8 35
36 ## Save the object in the page
VitantonioMessa 1.4 37 $doc.save()
Silvia Macovei 4.1 38 {{/code}}
VitantonioMessa 1.2 39
Silvia Macovei 4.1 40 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 41
Silvia Macovei 5.1 42 = 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
Silvia Macovei 4.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"
Silvia Macovei 4.1 53 {{/code}}
jeanvivienmaurice 1.13 54
jeanvivienmaurice 1.14 55 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.
56
Silvia Macovei 4.1 57 {{code}}
58 #set($class = $obj.xWikiClass) ## access the class object representing SomeSpace.SomeClass
jeanvivienmaurice 1.14 59 #foreach($prop in $class.properties) ## go through all properties
60 <dt> *${prop.prettyName}* </dt>
61 <dd>$doc.display($prop.getName())</dd>
62 #end
Silvia Macovei 4.1 63 {{/code}}
jeanvivienmaurice 1.14 64
Silvia Macovei 5.1 65 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 {{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.20 66
Silvia Macovei 4.1 67 {{code}}#includeForm("spacename.docname"){{/code}}
Vincent Massol 3.1 68
Silvia Macovei 5.1 69 See [[the reference for the includeForm() macro>>code:Macros.IncludeFormMacro]].
Vincent Massol 3.1 70
Silvia Macovei 5.1 71 = Include a Velocity page into another Velocity page =
Silvia Macovei 4.1 72
Silvia Macovei 5.1 73 See [[Include In Velocity>>DevGuide.IncludeInVelocity]].
Silvia Macovei 4.1 74
Silvia Macovei 5.1 75 = Redirecting to another page =
Silvia Macovei 4.1 76
Vincent Massol 3.1 77 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.
78
79 Example:
80
Silvia Macovei 4.1 81 {{code}}$response.sendRedirect($xwiki.getURL("Space.Page")){{/code}}

Get Connected