16.01.2013 Views

Microsoft Sharepoint Products and Technologies Resource Kit eBook

Microsoft Sharepoint Products and Technologies Resource Kit eBook

Microsoft Sharepoint Products and Technologies Resource Kit eBook

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1062 Part X: <strong>Microsoft</strong> Office 2003 Integration with SharePoint <strong>Products</strong> <strong>and</strong> <strong>Technologies</strong><br />

Submitting a Form Programmatically<br />

In InfoPath, you can set up a form template so that the users are able to submit the<br />

data in their form directly to an HTTP location, a database, or a Web service. If this<br />

feature is enabled, the Submit comm<strong>and</strong> is available on the File menu in the Fill Out<br />

Form mode. To programmatically specify the URL for submitting the InfoPath form<br />

to the SharePoint form library, add a script to the form’s OnSubmitRequest event<br />

h<strong>and</strong>ler, as described in the following procedure.<br />

1. In InfoPath design mode, open the form template.<br />

2. On the Tools menu, select Submitting Forms.<br />

3. Select Enable submit, select Submit using a custom script from the Submit<br />

drop-down list box, <strong>and</strong> then click OK to open the <strong>Microsoft</strong> Script Editor.<br />

4. Add a script similar to the following to the OnSubmitRequest event h<strong>and</strong>ler<br />

for the form:<br />

function XDocument::OnSubmitRequest(eventObj)<br />

{<br />

// If the submit operation is successful, set<br />

// eventObj.ReturnStatus = true<br />

var blnSubmitSuccess = false;<br />

var fFileExists = false;<br />

// Specify the URL of the file you want to submit here.<br />

var strUrl = “http://MyServer/MyFormLibrary/MyForm.xml";<br />

try<br />

{<br />

// Create an XMLHTTP object to transport the file.<br />

var objXmlHttp = new ActiveXObject(“MSXML2.XMLHTTP.5.0”);<br />

// Attempt to open to a form using the specified URL to<br />

// check if a form with the same name already exists<br />

// in the form library.<br />

try{<br />

objXmlHttp.Open(“HEAD", strUrl, false);<br />

objXmlHttp.Send();<br />

fFileExists = true;<br />

}catch(e)<br />

{<br />

XDocument.UI.Alert(“Submit failed due to the following error<br />

.\n\n” + ex.number + “ - “ + ex.description + “\n\nThe file already exist”);<br />

fFileExists = false;<br />

}<br />

// If no form at the same URL has been found, proceed<br />

// to submit. If you need logic to replace the previous<br />

// file, you can call<br />

// objXmlHttp.Open(“DELETE", strUrl, false) to delete<br />

// the form from the form library before proceeding.<br />

if (objXmlHttp.Status == 404 || !fFileExists)<br />

{<br />

// Submit the form to the form library.<br />

objXmlHttp.Open(“PUT", strUrl, false);<br />

objXmlHttp.Send(XDocument.DOM.xml);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!