Wednesday, November 7, 2007

Active Server Page Submission in the Data Submission Developer Sample Form

One of the ways in which a Microsoft Office InfoPath form can implement a data submission process is to submit the form's data to an Active Server Page (ASP or ASPX) that is hosted on a Web server such as Microsoft Internet Information Services (IIS). The data submission process is enabled through the Submitting Forms dialog box, which is opened by clicking Submitting Forms on the Tools menu while in design mode.

There are two approaches to enabling data submission with an ASP page: one is to specify the Uniform Resource Locator (URL) to the ASP page in the URL box that is displayed by clicking Submit through HTTP in the Submit list. The other is to perform the submission programmatically with script by clicking Submit using custom script in the Submit list and then adding custom script in the Microsoft Script Editor (MSE). In either case, the form's data is sent to the ASP page using HTTP, and the ASP page performs some processing of the data that was submitted.

In the Data Submission developer sample form, submission to an ASP page is implemented using custom script, which is contained in the OnSubmitRequest event handler that is created from the Submitting Forms dialog box.

Internet 2010The Data submission developer sample form performs three steps during the ASP data submission process, including determining the submission method, sending the data to the ASP page, and processing the data with the ASP page.

Determining the submission method

The first step that the Data Submission developer sample form takes in its implementation of a custom data submission process is to determine the submission method to use. The submission method is determined by the contents of the config.xml file that is included in the form files for the Data Submission form template. Using the elements contained in config.xml, you can specify whether the sample form uses an ASP page or an XML Web service for its data submission method.

Note The config.xml file is a feature of the Data Submission developer sample form and is not a built-in feature of InfoPath.

The following code represents the complete contents of config.xml:

There are three elements in the Config.xml file:

To specify the data submission method, simply modify the elements of the config.xml file using the examples contained in the comments of the file. The default Web server name used in the file is localhost, but this should be changed to match your own server name if the server is not running locally. For more information about configuring the Web server, see Configuring the Developer Sample Forms.

Note To modify the config.xml file, you'll need to extract the sample form's component files to a folder location on your hard disk. The file cannot be modified when the Data Submission developer sample form is open because it is write-protected.

Sending the data to the ASP page

When you click Test Submit on the File menu while filling out the Data Submission developer sample form, the script in the OnSubmitRequest event handler checks the submission method specified in config.xml, and if it is ASP, calls the AspPost custom function. This custom function takes two parameters:

Using the specified parameters, the AspPost function attempts to send the form's underlying XML document to the ASP page using the following code:

function AspPost(objXmlHttp, strUrl) { // post xml document to strUrl objXmlHttp.open("POST", strUrl, false); try { objXmlHttp.send(XDocument.DOM.xml); } catch(ex) { XDocument.UI.Alert("Could not post (ASP) document to " + strUrl + "\r\n" + ex.number + " - " + ex.description); return false; } ... }

The code first uses the open method of the XMLHTTP object to open the ASP page specified by the strUrl parameter; then it uses the send method to post the form's underlying XML document to the ASP page. The form's underlying XML document is obtained using the DOM property of the InfoPath object model's XDocument object, which returns a reference to an XML Document Object Model (DOM) containing the data of the form. Because the entire contents of the form's underlying XML document is to be sent to the ASP page, the xml property of the XML DOM is used.

If the AspPost function is unable to send the data to the ASP page, the Alert method of the InfoPath UI object displays an error message.

Note In addition to the preceding code, the AspPost function also contains script for checking the response returned by the ASP page. If the returned response indicates that an error has occurred, that error is displayed.

Processing the data with the ASP page

After the form's data is submitted to the ASP page, the ASP page takes over the processing of that data. It first checks the value of the blnSubmitSuccess element of the submitted XML document, and if that value is False, returns an error message and discontinues processing of the XML document. If the value is True, the ASP page uses the FileSystemObject object to save the XML document to a virtual directory on the Web server with the default file name SubmittedDocument.xml; then a successful response message is returned.

The following is the part of the ASP code that is used to save the XML document to the virtual directory on the Web server:

// Save the submitted XML document to disk. var objFso = Server.CreateObject("Scripting.FileSystemObject"); var strFileName = Server.MapPath(".") + "\\"+ "SubmittedDoc.xml"; if (objFso.FileExists(strFileName)) { objFso.DeleteFile(strFileName); } // Insert the <submitMethod> element in the XML document. var objSubmitMethodNode = objXmlDoc.selectSingleNode("//submitMethod"); if (!objSubmitMethodNode) { // If the <submitMethod> element was not present in the XML document, // then create it and add it as the last child. objSubmitMethodNode = objXmlDoc.createElement("submitMethod"); objXmlDoc.documentElement.insertBefore(objSubmitMethodNode, null); } objSubmitMethodNode.text = "ASP" // Insert the <dteSubmitDate> element in the XML document. var objSubmitDateNode = objXmlDoc.selectSingleNode("//dteSubmitDate"); if (!objSubmitDateNode) { // If the <dteSubmitDate> element is not present // in the XML document, create it. objSubmitDateNode = objXmlDoc.createElement("dteSubmitDate"); objXmlDoc.documentElement.insertBefore( objSubmitDateNode, objSubmitMethodNode); } var dteNow = new Date(); objSubmitDateNode.text = formatDate(dteNow); objXmlDoc.save(strFileName); // Return submission success. objNewEl.text = "true"; objCurEl.appendChild(objNewEl); objReturnedXmlDoc.save(Response); Response.Status = "200 OK"; Response.End();

In addition to simply persisting the submitted XML document to a virtual directory on the Web server, the ASP page also inserts two XML elements into the saved document:

If you open the SubmittedDocument.xml file using InfoPath, both of the added XML elements will be displayed in the form.

No comments:

Internet Blogosphere