Wiki source code of XWiki Scripting API Guide

Version 29.2 by Vincent Massol on 2015/09/09

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 This guide covers the main XWiki APIs that you can use from scripts in wiki pages. It's not meant to be comprehensive. For that you'll need to check the [[XWiki Reference API page>>API]].
6
7 Note that while most examples are written in Velocity you can use [[any other scripting language>>DevGuide.Scripting]] to access the same APIs.
8
9 = Querying documents =
10
11 See the [[Query Module>>extensions:Extension.Query Module]] for examples on how to perform queries on the wiki using a scripting language.
12
13 = Create a new Document =
14
15 For example in Velocity:
16
17 {{code}}
18 ## Note that getDocument() creates a Document if it doesn't exist. This can be checked with $newDoc.isNew()
19 #set ($newDoc = $xwiki.getDocument('MySpace.MyPage'))
20 ## Set its content (for example)
21 #set ($discard = $newDoc.setContent("new content"))
22 ## The second parameter to save() indicates whether the save is a minor edit or not
23 #set ($discard = $newDoc.save("some comment explaining the save", true)
24 {{/code}}
25
26 = Accessing the request =
27
28 You can access the HTTP Request by accessing the ##com.xpn.xwiki.web.XWikiServletRequest## object through the ##request## script variable.
29
30 For example in Velocity, to access an ##action## HTTP parameter passed in the request you would write:
31
32 {{code language="velocity"}}
33 $request.action
34 {{/code}}
35
36 Note that this is a shortcut to:
37
38 {{code language="velocity"}}
39 $request.get("action")
40 {{/code}}
41
42 = Getting external content =
43
44 You can use the following APIs to get content located at external URLs:
45
46 {{code language="java"}}
47 public String getURLContent(String surl, String username, String password) throws IOException
48 public String getURLContent(String surl) throws IOException
49 public String getURLContent(String surl, String username, String password, int timeout) throws IOException
50 public String getURLContent(String surl, int timeout) throws IOException
51 public byte[] getURLContentAsBytes(String surl, String username, String password)
52 public byte[] getURLContentAsBytes(String surl) throws IOException
53 {{/code}}
54
55 For example in Velocity:
56
57 {{code language="velocity"}}
58 $xwiki.getURLContent("http://google.com")
59 {{/code}}
60
61 = Add objects to a page =
62
63 Here is a piece of Velocity script to show how is possible to store a new object in one page:
64
65 {{code language="velocity"}}
66 ## Create an object
67 #set($obj = $doc.newObject("XWiki.SomeClass"))
68 $obj.set("field1",$value1)
69 $obj.set("field2",$value2)
70
71 ## Save the object in the page
72 $doc.save()
73 {{/code}}
74
75 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.
76
77 = Access objects in a page =
78
79 Here is a piece of Velocity script to show how it is possible to access an object attached to the page, and read its fields :
80
81 {{code language="velocity"}}
82 ## Retrieve the first object (index [0]) among all objects attached to this page and of a certain class
83 #set($obj = $doc.getObject('SomeSpace.SomeClass'))
84
85 ## Retrieve the raw value of the propertty "field1" for this object, provided
86 ## a property called "field1" is actually defined in the class of this object
87 #set($rawValue = $obj.getProperty('field1').value)
88 SomeSpace.SomeClass[0] : field1 = "$rawValue"
89
90 ## return the property type: String, TextAreaDate, Boolean, ...
91 $obj.get('field1').classType
92 {{/code}}
93
94
95 You can also go through all the properties of an object, without knowing their respective names. That's how the default Class Sheet works, when you create a class using the Class Wizard.
96
97 {{code language="velocity"}}
98 #set($class = $obj.xWikiClass) ## access the class object representing SomeSpace.SomeClass
99 #foreach($prop in $class.properties) ## go through all properties
100 <dt> *${prop.prettyName}* </dt>
101 <dd>$doc.display($prop.getName())</dd>
102 #end
103 {{/code}}
104
105 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 that uses the ##display(propertyName)## method to access properties of an object attached to the including page and you want to include it somewhere else you have to use the ##includeForm()## Velocity macro in the including script:
106
107 {{code language="velocity"}}
108 #includeForm("spacename.docname")
109 {{/code}}
110
111 See [[the includeForm() macro>>extensions:Extension.Include Form Macro]] for more information.
112
113 = Access objects from any page and loop over all objects of same Class =
114
115 Here is a piece of Velocity script to show how it is possible to access an object attached to the page from another page, and read its fields:
116 (It is similar than previous code except you must "call" page before with ##$xwiki.getDocument##.)
117
118 {{code language="velocity"}}
119 ## get the document which has the object (only one here) - this is the page where I can see things in the object editor
120 ## Retrieve the first object (index [0]) among all objects attached to the page MySpace.MyDocWithMyClassObjects and of a certain class MySpace.MyClass
121 #set( $MyDoc = $xwiki.getDocument("MySpace.MyDocWithMyClassObjects"))
122 ## get the document wich contains the class definition: this page has entries in the class editor
123 #set( $class = $xwiki.getClass("MySpace.MyClass"))
124 #foreach($prop in $class.properties) ## go through all properties
125 * ${prop.prettyName} : $MyDoc.display($prop.getName())
126 #end
127 {{/code}}
128
129 If ##MySpace.MyDocWithMyClassObjects## have many attached objects of ##MySpace.MyClass## class (with different value)
130 {{image reference="APIGuide-MyDocWithMyClassObjects.png"/}} {{image reference="APIGuide-ResultOfLoops.png"/}}
131
132 {{code language="velocity"}}
133 ## if you have more than one object on a page, you will have to loop over them and use "$doc.use"
134 #set($MyDoc = $xwiki.getDocument("MySpace.MyDocWithMyClassObjects"))
135 #set($class = $xwiki.getClass("MySpace.MyClass"))
136 ## loop over all objects
137 #foreach($obj in $MyDoc.getObjects("MySpace.MyClass"))
138 * Object number $velocityCount
139 #set($discard = $MyDoc.use($obj))
140 #foreach($prop in $class.properties) ## go through all properties
141 ** ${prop.prettyName} : $MyDoc.display($prop.getName())
142 #end
143 #end
144 {{/code}}
145
146 = Include a Velocity page into another Velocity page =
147
148 See the [[Include In Velocity tutorial>>DevGuide.IncludeInVelocity]].
149
150 = Redirecting to another page =
151
152 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.
153
154 Example:
155
156 {{code language="velocity"}}
157 $response.sendRedirect($xwiki.getURL("Space.Page"))
158 {{/code}}
159
160 For more examples, see the [[Redirect Snippet>>extensions:Extension.Redirect]].
161
162 = Add an Attachment to a page =
163
164 For example, in Velocity:
165
166 {{code}}
167 {{velocity}}
168 #set($content = "This is some small arbitrary content")
169 #set($discard = $doc.addAttachment("myfile.txt", $content.getBytes()))
170 #set($discard = $doc.save("some comment"))
171 {{/velocity}}
172 {{/code}}

Get Connected