ShowTable of Contents
Get and set a field on the XPage
Server JS
getComponent("elementName").getValue();
getComponent("elementName").setValue("something");
Note: you cannot access or set fields with visible=false. If you want to work with a field, but don't want to display it, give the field a CSS class with "display:none".
Get submitted value before validation
getComponent("elementName").getSubmittedValue()
Client JS
document.getElementById("#{id:elementID}").value;
Get a computed field's value:
document.getElementById("#{id:elementID}").innerHTML;
(thanks, Jerry Shelly!).
Access backend Notes document
Fields on the XPage are bound to a datasource. If the datasource represents a Notes document, you can access that with:
var doc:NotesDocument = datasourcename.getDocument();
Note: the variable "doc" is typed with "NotesDocument" here so that the IDE can give us a help for properties and methods.
Note: use datasourcename.getDocument(true) to get a backend document which has the changes which were applied to the UI document (kind of NotesUIDocument.refresh).
To work with a field, you can use
var doc:NotesDocument = datasourcename.getDocument();
doc.replaceItemValue("somefield", "somevalue");
Or to get a field which is in the backend doc but not on the XPage:
var v = datasourcename.getValue("fieldname");
Work with a document in the click event of a submit button
You can access the document and set fields as described above in the onClick event of a submit button.
But you have to redirect to another XPage after that by yourself using context.redirect() then.
Run a agent on document save
Something like the "WebQuerySave" agent in classic domino development. In datasource -> querySave event run
var agent:NotesAgent=database.getAgent("agentname");
agent.run(datasourcename.getDocument().getNoteID());
Im Agent bekommt man das Dokument dann via notesDatabase.GetDocumentByID( NotesAgent.ParameterDocID )
In the agent you get the noteID of the saved document with
set session = new notesSession
set database = session.currentDatabase
set agent = session.currentAgent
set doc = database.getDocumentByID(agent.parameterDocID);
Check for edit mode and set edit mode
<datasource>.isEditable()
returns true if is in editmode.
To set a mode the the "current" document, you can use
context.setDocumentMode("edit");
With the possible parameters "edit", "readOnly", "autoEdit" and "toggle".
(Thanks to Michelle).
To set mode for a specific document data source, use
context.setDocumentMode(String nameOfDataSource, String mode)
First parameter is the name of the document data source as string (!), not the data source variable itself!
Second parameter is "edit", "readOnly", "autoEdit" or "toggle".
if the context.setDocumentMode() method does not work for your for some reason, you can use this:
function setEditMode(doc:NotesXSPDocument, editmode:boolean) {
try {
var cl:java.lang.Class = com.ibm.domino.xsp.module.nsf.NotesContext.getCurrent().getModule().getModuleClassLoader().loadClass("com.ibm.xsp.model.domino.wrapped.DominoDocument");
var method:java.lang.Method;
var paramTypes = new Array();
paramTypes[0] = java.lang.Boolean.TYPE;
method = cl.getMethod("setEditable", paramTypes);
var params = new Array();
params[0] = new java.lang.Boolean(editmode);
method.invoke(doc, params);
} catch (e) {
print("setEditMode error: "+e);
}
}
Update 15th march 2011: code updated since the old cold didn't worked in 8.5.2 anymore. This function uses undocumented features and may break in future versions of Domino.
Use context.setDocumentMode() if possible.
As an alternative, you can use the tip from John Mackey from our blog
:
var url=view.getPageName()+"?action=editDocument&documentId="+document.getNoteID();
context.redirectToPage(url, false);
Check if a field is disabled
getComponent("<fieldname>").disabled
returns true if the field is disabled.
Set fields in backend document in QuerySave event
XPage -> events -> Datasource-Name -> querySave: use
Datasource.replaceItemValue("fieldname", "value");
instead of
Datasource.getDocument().replaceItemValue("fieldname", "value");
Use dynamic value binding for a field
( Discovered by Tommy Valand
)
You can change / set the value binding for a field at runtime:
var application = facesContext.getApplication();
var scopeKey = "exampleKey"
var valueBinding = application.createValueBinding( '#{viewScope.' + scopeKey + '}')
getComponent( 'id-of-a-field' ).setValueBinding( 'value', valueBinding );
(In this example a viewScope field is used for value binding.)
Define dynamic value binding for a field at development time in the XML source
In the XML source of your XPage, you can write something like this:
<xp:inputRichText id="rtBody"
value="#{dominoDoc[42]}"/>
which would bind the RichText field to the 42nd field of the document datasource "dominoDoc". While using an index number here is of limited usage, you can also use a fieldname in the brackets:
<xp:inputRichText id="rtBody"
value="#{dominoDoc['myfield']}"/>
And finally you can use EML syntax to dynamically compute a fieldname, for example using an applicationScope variable:
<xp:inputRichText id="rtBody"
value="#{dominoDoc[applicationScope.bodyFieldName]}"/>
Set a group of fields to readonly
You can use a xp:panel to wrap a group of fields and set the readonly property to all of them at once:
<xp:panel disableTheme="true">
<xp:this.readonly><![CDATA[#{javascript:true}]]></xp:this.readonly>
... fields ...
</xp:panel>
Note the disableTheme="true" attribute which prevents rendering a div-tag for the panel. That means you can for example wrap some table rows inside such a panel, too.
Check if a panel is readonly
If you want to hide some components inside a panel depending on it's readOnly state, you can read the state with:
getComponent("myPanel").isReadonly()
Use custom converter to change submitted and displayed value of a field
Add a xp:customConverter to a field and implement the getAsObject and getAsString properties.
getAsObject is called when the user gave a value, bevore the value is stored to the appropriate NotesItem.
getAsString is called before the value is displayed in the field.
There is the standard variable "value" which contains the actual value of the field.
Example: limit the value of a field to 20 chars:
in getAsObject:
@Left(value, 20)
in getAsString:
value
(in this exampel we don't want to alter the displayed value but the stored value, therefore we just return the current value in getAsString).
Set different style to required and invalid fields
Required fields get the attribute aria-required=true and invalid fields get the attribute aria-invalid=true automatically.
You can style them with CSS with code like this:
[aria-invalid=true] { background-color: #fee; border-color: red; }
[aria-required=true] { background-color: #ffe; }
Found by Tommy Valand
Dynamically change data binding of a field with SSJS
You can change the data binding for a field (component) dynamically with SSJS.
For example in a custom control, you can define a custom parameter to define the data binding for a field inside the custom control.
Example code for the "afterPageLoad" event of a custom control:
if (compositeData.fieldDataBinding) {
var application = facesContext.getApplication();
var valueBinding = application.createValueBinding( "#{"+compositeData.fieldDataBinding+"}");
getComponent( 'fileUpload' ).setValueBinding( 'value', valueBinding );
}
In this example, the custom control has a custom property "fieldDataBinding" and a file upload control with the id "fileUpload".
The file upload control has a default data binding to "document.Body", but when the "fieldDataBinding" custom paramter is set, the default data binding will be changed.
In the XPage, the custom parameter can be set to "document.files" or any other EML string.
Hold documents in memory
You store documents in memory, for example to do some caching. But you cannot do this with standard Notes objects, you need the DominoDocument/NotesXSPDocument wrapper.
See this blog entry from Frank van der Linden
.