Create Page With Object

Last modified by Vincent Massol on 2021/03/18 11:28

cogShow how to programmatically add a document with an object
TypeSnippet
Category
Developed by

xwiki:XWiki.JaredScott

Rating
1 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Note that all of the names used should be modified to suit your own needs. It took me a few hours to pull this all together, so hopefully it helps someone!

{{velocity}}
##Testing creating a document programatically
#set($newDoc = $xwiki.getDocument("MySpace.NewPage"))

##Set a title
$newDoc.setTitle("this is my title")

##Not necessary but good to know
$newDoc.setParent("MySpace.WebHome")

##This is how you can use a template
$newDoc.setContent('{{include document="MySpaceCode.MySpaceTemplate"/}}')

##Create a new object on the document
#set($newObj = $newDoc.newObject("MySpaceCode.MySpaceClass"))

##Set a field to a particular value
$newObj.set("my_field","This is some test content")

$newDoc.save()

{{/velocity}}

Get Connected