How to submit an InfoPath form to a Windows SharePoint Services document library

on October 27, 2006

Hi,

That is the last part of a two posts set of articles. We’ve first seens how-to debug MS Script in InfoPath (You can find it here: http://www.e-soft.ca/blogs/index.php?title=how_to_debug_ms_script_jscript_in_ms_inf_2003&more=1&c=1&tb=1&pb=1 ).

This article show you somehting very important. How to submit a newly filled form to a doclib. If you do nto customize your forms do submit them, you need to show your users how to save the form in the document library. That may seems obvious but it isn’t. Because they also need to define the file name and they want to be sure that they did everything properly. There is also a risk that there overwrite an existing form.

Many information for this articale come frmo MSDN article 826993:
http://support.microsoft.com/kb/826993/en-us

First thing you want to do is to create a button to submit the form.

Submit the form data to a SharePoint Services document library
You can use script or the UI to submit the form data to the SharePoint Services document library.

Use script to submit the form data to a SharePoint Services document library in InfoPath 2003
1. On the Tools menu, click Submitting Forms.

The Submitting Forms dialog box appears.
2. In the Submitting Forms dialog box, click Enable Submit.
3. In the Submit box, click Submit Using Custom Script.

Here you can also tick the checkbox to provide a caption to the submit button. I Use “Submit the form.”

In Send options, you can tick “Close the form” after submit

4. Click Open Microsoft Script Editor, and then click OK. If it is checked, uncheck the option to display a message to the user. We will handle this ourself with the script. It makes it very clean for the user.

so… Microsoft Script Editor starts.
5. Modify the OnSubmitRequest event as follows.

Code:

function XDocument::OnSubmitRequest(eventObj)
{
// Modified by YANN
// If the submit operation is successful, set
// eventObj.ReturnStatus = true.
var fSuccessful = false;
// Set the URL of the file that you want to submit here.
var now = new Date();
var time = getTimeString(now);
time = time.replace(":","");
time = time.replace(":","");
time = time.replace(".","");
var date = getDateString(now);
date = date.replace("-","");
date = date.replace("-","");
var fileName = date + "-" + time + ".xml";
var strUrl = "https://host.domain.com/sites/SiteName/DocLibName/" + fileName;
//debugger;
try
{
// Create an xmlhttp object.
var oXmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
// See whether the document with the same name already exists in the Windows SharePoint Services (WSS) document library.
oXmlHttp.Open("HEAD", strUrl, false);
oXmlHttp.Send();
// No document with the URL has been found. Continue to submit.
// If you must replace the original file, you must call
// oXmlHttp.Open("DELETE", strUrl, false) to delete the document
// in the WSS document library.
if (oXmlHttp.Status == 404)
{
// Put the document in the WSS document library.
oXmlHttp.Open("PUT", strUrl, false);
oXmlHttp.Send(XDocument.DOM.xml);
// A 200 status code or a 201 status code indicates that the form has been submitted successfully.
if (oXmlHttp.Status == 200 oXmlHttp.Status == 201)
{
fSuccessful = true;
}
}
}
catch (ex)
{//XDocument.UI.Alert("Error: " + ex.Message);
}
if (fSuccessful)
{
XDocument.UI.Alert("Your form was submitted successfully.");
eventObj.ReturnStatus = true;
}
else
{
XDocument.UI.Alert("There was a problem sending your form. Therefore, your form was NOT saved.");
eventObj.ReturnStatus = false;
}
}

From line 9 to 17, We build the filename from date and time. This is what I do to get a unique name for the forms. Of course, you can modify this to fit your needs.

Here is a link to JScript Reference at MSDN to help with the language:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/1e9b3876-3d38-4fd8-8596-1bbfe2330aa9.asp

Note You must change the value of the strURL variable to a valid URL in the code. IT needs to be a valid encoded URL.

6. Save the script. Close Script Editor.
7. Save your form template as SubmitToWSS.xsn.

Use the UI to submit the form data to a SharePoint Services document library in InfoPath 2003 Service Pack 1
1. On the Tools menu, click Submitting Forms.

The Submitting Forms dialog box appears.
2. In the Submitting Forms dialog box, click Enable Submit commands and buttons.
3. In the Submit to section, click SharePoint form library.
4. In the Submitting Forms dialog box, click Add.

The Data Connection Wizard starts.
5. Input your SharePoint Services form library link in the SharePoint form library box.

Your library link may look similar to the following:

http://ServerName/SiteName/DocumentLibraryName
6. Type the document name in the File name box.

You can use a document name such as TestForm.

Click Next.
7. In the Data Connection Wizard, click Finish.
8. In the Submitting Forms dialog box, Click OK.
9. Save your form template as “SubmitToWSS.xsn.”

Back to the top

Test your form
1. On the File menu, click Fill Out a Form.
2. In the Fill Out a Form task pane, click SubmitToWSS.
3. Complete the form.
4. On the File menu, click Submit.

If the form is submitted successfully, you receive a message that indicates this.

5. If you ticked the checkboxes as I recommanded earlier, this point is not used: “Quit InfoPath. If you are prompted to save your changes to the form, click No. “

6. Start Microsoft Internet Explorer. Locate your document form library.

For example, your document form library may be http://ServerName/SiteName/DocumentLibraryName.

A new item that is named in the document library.
7. Click the newly created item.
The form that you submitted opens in InfoPath 2003.

ShareThis