XPages - How to pass a Document Data Source to a Custom Control
Monday, August 17, 2009 at 10:21AM In a previous article on my blog, I explained how you can use the undocumented currentDocument data source object to get the data source for the XPage from a Custom Control. This works great if the XPage has one data source. What if you have multiple data sources? For example, you want to include a custom control within a repeat control that is accessing multiple documents from a document collection or a view. The currentDocument variable would only point to the first data source on the XPage.
At first glance, looking at the data types that are available for parameters in a Custom Control, you cannot pass a data source object. The type list drop down is a list of standard JavaScript variable types.
But, if you click on the folder icon next to the Type field you will see an extended list of type available.
Then from your XPage you can pass the datasource using computed JavaScript, such as: "OrderDetails.getDocument();"
*updated - see note below
Binding fields on a Custom Control to a dynamic datasource
The second part of this can be tricky. On your Custom Control you cannot use the "Simple Data Binding" option to bind your fields since you have no datasource set for the Custom Control, the datasource is dynamic.
The perfect solution for this is to use the Advanced -> Expression Language (EL) binding. For example, to bind the field to the "Comment" field, we would use "compositeData.docdatasource.Comment". Where....
- compositeData holds all the parameters for the Custom Control
- docdatasource is the parameter that we created that receives the datasource
- Comment is the field name on the Notes form
Now, with that said, there is an issue with this in 8.5. DDE tries to convert the EL expression to a Simple data binding when you save and reopen the Custom Control. So it works the first time you enter it, but loses the binding when you edit the Custom Control. However, it looks like this is fixed in 8.5.1.
BUT, no sweat, I have a workaround. If you select JavaScript for the binding type, select "Compute on page load", and then enter the formula as '#{compositeData.docdatasource.Comment}' it will continue to work.
Here's an example of the Custom Control used with multiple Data Sources in action:
You can download this example here.
*update - by passing the NotesXSPDocument instead of the NotesDocument, the fields will automatically handle whether the document is in read or edit mode.
To do this, select "ModelDataSource" as the type for the property. Then when passing the datasource from your XPage change the JavaScript from "OrderDetails.getDocument()" to just "OrderDetails".
-John
Ccustom controls,
XPages,
data source in
Development 