[xwiki-devs] How to set a properties value on a newly created document
Hi, I have a document where the user set the name of a document in a textfield to create a new document as described in the FAQ tutorial. How is it possible to set a property of the document's object programmatically? In my case I have a property "id_project" of my class "project". I want to set the value (an automatically generated id value) of this property in the new document right after creating it, without user interaction. It should save the id value with the other values the user defines before saving the document for the first time. Thanks a lot! -- View this message in context: http://xwiki.475771.n2.nabble.com/How-to-set-a-properties-value-on-a-newly-c... Sent from the XWiki- Dev mailing list archive at Nabble.com.
Hi, add DocumentCreatedListener to the List from getEvents() in Example http://platform.xwiki.org/xwiki/bin/view/DevGuide/GroovyNotificationTutorial and use @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; XWikiContext context = (XWikiContext) data; if (event instanceof DocumentCreatedEvent) { // Do what you need here ;-) } } -- View this message in context: http://xwiki.475771.n2.nabble.com/How-to-set-a-properties-value-on-a-newly-c... Sent from the XWiki- Dev mailing list archive at Nabble.com.
Thank you very much, Matthias - but I cannot figure out how to use it. I have added the following code to my Class Sheet of the project class to examine the functionality. What I try is to print the variable content on save and update of a project document, but there is no output visible. Could you please give me a hint? Thanks in advance! {{groovy}} import org.xwiki.observation.* import org.xwiki.observation.event.* import com.xpn.xwiki.web.* import com.xpn.xwiki.* import com.xpn.xwiki.doc.XWikiDocument; import org.xwiki.component.annotation.Component; import org.xwiki.component.annotation.Requirement; import org.xwiki.component.manager.ComponentLookupException; import org.xwiki.component.manager.ComponentManager; import org.xwiki.model.reference.DocumentReference; import org.xwiki.observation.EventListener; import org.xwiki.observation.ObservationManager; import org.xwiki.observation.event.DocumentSaveEvent; import org.xwiki.observation.event.Event; class ProjectCreatedEventListener implements EventListener { def xwiki def context ProjectCreatedEventListener(xwiki, context) { this.xwiki = xwiki this.context = context } String getName() { // The unique name of this event listener return "projectcreated" } List<Event> getEvents() { // The list of events this listener listens to return Arrays.asList(new DocumentSaveEvent()) } // Called by the Observation Manager when an event matches the list of events returned // by getEvents() void onEvent(Event event, Object source, Object data) { XWikiDocument document = (XWikiDocument) source; String wikiName = document.getDocumentReference().getWikiReference().getName(); println wikiName; } } // Register against the Observation Manager def observation = Utils.getComponent(ObservationManager.class) observation.removeListener("projectcreated") def listener = new ProjectCreatedEventListener(xwiki, xcontext) observation.addListener(listener) {{/groovy}} -- View this message in context: http://xwiki.475771.n2.nabble.com/How-to-set-a-properties-value-on-a-newly-c... Sent from the XWiki- Dev mailing list archive at Nabble.com.
Fixed value can be set by document template which is provided to create in new Class creation wizard. (Open template document in object editor mode and enter necessary values). Dynamic value can be set using document sheet http://extensions.xwiki.org/xwiki/bin/view/Extension/Sheet+Module with following Velocity code: {{velocity}} #if($doc.isNew()) #set($obj=$doc.getObject('Space.YourObjectClass')) $obj.set('property','value)) #end More at http://platform.xwiki.org/xwiki/bin/view/DevGuide/APIGuide Valdis
Hi,
I have a document where the user set the name of a document in a textfield to create a new document as described in the FAQ tutorial.
How is it possible to set a property of the document's object programmatically? In my case I have a property "id_project" of my class "project". I want to set the value (an automatically generated id value) of this property in the new document right after creating it, without user interaction. It should save the id value with the other values the user defines before saving the document for the first time.
Thanks a lot!
-- View this message in context: http://xwiki.475771.n2.nabble.com/How-to-set-a-properties-value-on-a-newly-c... Sent from the XWiki- Dev mailing list archive at Nabble.com. _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
participants (3)
-
Matthias Wegner -
spoolox -
Valdis Vītoliņš