There are many options for submitting data from InfoPath forms to SharePoint, SQL Server, Web Services etc, there aren’t so many obvious options for submitting data from an InfoPath form to a SharePoint list however.
One option is to use the SharePoint Object Model to create list items using data collected in the form, in this context we are using InfoPath as a list item data entry form, just like NewForm.aspx. Why? well there can be many reasons, it may be that it’s easier to create a complex UI using InfoPath instead of creating a new List Form, indeed it may be that you don’t actually want a new list form, just a different means of entering list item data.
There is one caveat, to use the SharePoint Object Model from InfoPath forms, the form must be browser compatible, deployed to a site collection and opened in the browser, rather than the InfoPath client.
To get started we’ll create a form which creates a new Task item in a site. The Site URL will be passed to the form as a query string parameter named “SaveLocation”.
Create a new InfoPath form and check the browser Compatible check-box.

Set the Form Options, in the Security and Trust option choose either Domain or Full Trust, forms with code-behind will only work with Domain or Full Trust selected. Set your other form otpions as required.

Create the Forms VSTA project for the forms code-behind.

We will use the form Submit event to create the new SharePoint list item, but we will perform the Submit action entirely in code-behind, so we need to set the forms submit options (Tools | Submit Options).
Select the option to “Perform custom action using Code…”, set other options as required and click the Edit Code… button to create the submit event handler in your code-behind. Then click OK.

In your forms VSTA project you’ll need to set a reference to Microsoft.Sharepoint.dll

Create 2 text elements in your forms Datasource;

And create the form UI;

Set the button Action property to Submit;

In your VSTA code-behind project, we now need to modify the FormEvents_Loading method to capture the SaveLocation query string parameter and store it in the form datasource for use later when we create the list item.
// set target site location using the "SaveLocation" query string parameter XPathNavigator xnTargetSite = MainDataSource.CreateNavigator().SelectSingleNode("//my:targetSite", NamespaceManager); if (xnTargetSite != null) { if (this.Application.Environment.IsBrowser && e.InputParameters.ContainsKey("SaveLocation")) xnTargetSite.SetValue(e.InputParameters["SaveLocation"]); else xnTargetSite.SetValue(""); }
Next we need to write the code which will perform the Submit action, in your code-behind project locate the FormEvents_Submit method and drop in the follwoing code.
// write object model code to save sample Task listitem at the target site specified XPathNavigator xnTargetSite = MainDataSource.CreateNavigator().SelectSingleNode("//my:targetSite", NamespaceManager); if (xnTargetSite == null || string.IsNullOrEmpty(xnTargetSite.Value)) { // error: cancel the submit event e.CancelableArgs.Cancel = true; e.CancelableArgs.Message = "No Target Site was Specified!"; return; } try { using (SPSite site = new SPSite(xnTargetSite.Value)) using (SPWeb web = site.OpenWeb()) { web.AllowUnsafeUpdates = true; // !! allow updates from a HTTP GET !! // create new item in Tasks list SPList taskList = web.Lists["Tasks"]; SPListItem taskItem = taskList.Items.Add(); taskItem["Title"] = MainDataSource.CreateNavigator().SelectSingleNode("//my:taskTitle", NamespaceManager).Value; taskItem.Update(); web.AllowUnsafeUpdates = false; } // success: don't cancel the submit event e.CancelableArgs.Cancel = false; } catch (Exception ex) { // error: cancel the submit event e.CancelableArgs.Cancel = true; e.CancelableArgs.Message = "Exception: " + ex.Message; }
What this method does is grab the target site location from a datasource element (set during the FormEvents_Loading) and uses it to create an SPSite instance. It’s then a familiar task to create the Task list item.
Another caveat here is that you must set SPWeb.AllowUnsafeUpdates to true since the update will be performed during a HTTP GET operation.
After building the VSTA project, the next step is to publish the form to SharePoint and hook it up into the user interface.
Since the form doesn’t “Submit” the form data as an XML file to a SharePoint Document Library etc, we aren’t going to publish it as a content type or associate it with a Document Library and take advantage of the automatic UI hook-in, instead we’ll create a link on a page using the Content-Editor Web Part.
In part 2 we’ll cover publishing the form and creating the user interface link.
set the Form Options, in the Security and Trust option choose Full Trust since forms with code-behind must have full-trust. Set your other form otpions as required.
Forms with code behind also work with domain security..then why did you say that it should be full trust..i might also be wrong or i misunderstood the statement..can you plz clarify.
Thanks for the lovely post though..
Sid, yes an omission on my part, forms with code-behind will also work with domain trust, I’ll update the post