You want to save the data that is entered into an InfoPath form to a SharePoint list, but InfoPath does not allow you to submit data to a SharePoint list.
This is one of the useful feature of Microsoft SharePoint is its ability to create and maintain lists that users can leverage to store, view, and share items with other users of the SharePoint site. It is often desirable to be able to view these items and to add additional items with an InfoPath form.
You can use SharePoint object model to insert an Item to a SharePoint List.
Note: Users must have proper permissions assigned to them to be able to add items to the SharePoint list.
Below I am going to explain you with an example of Address list.
Add the SharePoint data connection to receive data:
- Choose Data Connections from the Data Tab from the Ribbon, and then click Add.
- In the Data Connection Wizard, select Receive Data, and then click Next.
- Select SharePoint Library or List, and then click Next.
- Type the name of your SharePoint site into the text box, and then click Next.
- Select Contacts from the list of libraries, and then click Next.
- Select the Title, Street, District, State and Country fields, deselect every other field, and then click Next.
- Click Finish.
Design the form
Switch back to InfoPath Designer and Select the Address Datasource(this the one which is generated above)
Choose Repeated table as shown in the Screen Shot below
Change the property of the textbox to read only for all the textboxes in the repeating table
Add Name, Street, District, State and Country Fields to the Main Data Source.
And Finally the forms looks like the below screen shot.
Add the submit functionality to the button
Right click on the Button and select the button properties and click on ‘Edit Form Code”. Visual Studio is loaded with the button click event. Add a reference of Microsoft.SharePoint DLL.
SharePoint stores lists using a unique identifier, called a GUID, for each list. You must know the GUID for your specific implementation of SharePoint in order to submit new items to it. We can use the receive data connection “Address” to get the SiteURL and ListGUID
Add the below code to the button click event
(Click on the Image to Enlarge)
Note: This code only Supports Browser Based Infopath forms, Does not support Infopath client
Now save and Publish the form as an Administrator-approved form template. Upload the published form to SharePoint and configure the Infopath Form to Forms Library.
Open the form and Submit the data, You will see a new item is added to the Address list of Your SharePoint Site.
There is one tiny issue! When you click on Submit, it does not refresh the repeating table, You are forced to refresh the page to see the newly added Address.
To fix this issue, Add a new field to the Main Data Source and call it Check, Once you Add a new item to list change the value of the Field to 1, Set a rule on the field that if the status is 1 query the Address DataSource, this will refresh the Repeating table. Step by step implementation is mentioned below:
Step 1: Create a new field with the name Check. With the default value as 0
Add the below line of code at the bottom of the code mentioned above
Need to setup rules On the check field so that if the value of the field is changed to 1 it should do a query on the Address data source.
On the run these actions select Query for data and select Address data source.
Again Select Add on the Run these actions and select Set a field´s value and set the check value to 0.
Again Select Add on the Run these actions and select Set a field´s value and set the Name value to “”.
Repeat the same to all the fields on the form.
Publish and you are done!!!