XDocument.OnLoad Event

InfoPath Developer Reference

Occurs after a Microsoft Office InfoPath 2007 form has been loaded, but before any views have been initialized.

Version Information
 Version Added:  InfoPath 2003



expression   An expression that returns a XDocument object.


Name Required/Optional Data Type Description
pEvent Required DocReturnEvent An event object that is used during a Microsoft Office InfoPath 2007 load or submission event.

Return Value


This event handler allows users to cancel an operation.

If the ReturnStatus property of the DocReturnEvent object is set to False, InfoPath cancels the loading of the form. If an error occurs in the scripting code for the OnLoad event handler, InfoPath ignores it and relies on the ReturnStatus property of the DocReturnEvent object. If the ReturnStatus property is not explicitly set, the default value of True is used.

Bb229737.vs_note(en-us,office.12).gif  Note
When the OnLoad event occurs, the view is not initialized and the XSL Transformation (XSLT) used for the view is not yet loaded. The XDocument object is not added to the XDocuments collection until after the OnLoad event has occurred. However, the XDocument object is available during the OnLoad event.


In the following example from the Sales Report sample form, the OnLoad event handler is used to determine whether the form has been digitally signed, and if it hasn't, to initialize some date values using a combination of scripting functions and custom functions:

  function XDocument::OnLoad(objEvent)
   // Avoid DOM updates when the document has been digitally signed.
   if (XDocument.IsSigned)

var today = new Date(); initializeNodeValue("/sls:salesReport/sls:date", getDateString(today)); initializeNodeValue("/sls:salesReport/sls:year", today.getFullYear()); }

This Onload event handler example depends on two custom functions: initializeNodeValue and setNodeValue.

  function initializeNodeValue(xpath, strValue)
    var xmlNode = getNode(xpath);
// Set the node value *ONLY* if the node is empty.
if (xmlNode.text == "")
    setNodeValue(xmlNode, strValue);


  function setNodeValue(xpath, value)
    var xmlNode = getNode(xpath);
if (!xmlNode)

// The xsi:nil needs to be removed before we set the value.
if (value != "" && xmlNode.getAttribute("xsi:nil"))

// Setting the value would mark the document as dirty.
// Let's do that if the value has really changed.
if (xmlNode.text != value)
    xmlNode.text = value;


See Also