manoj´ssharepointblog

SharePoint Blog

Add SharePoint List Items with InfoPath web form using SharePoint Object Model

Leave a comment

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.

Columns:

  • Name
  • Street
  • District
  • State
  • Country

Add the SharePoint data connection to receive data:

  1. Choose Data Connections from the  Data Tab from the Ribbon, and then click Add.
  2. In the Data Connection Wizard, select Receive Data, and then click Next.
  3. Select SharePoint Library or List, and then click Next.
  4. Type the name of your SharePoint site into the text box, and then click Next.
  5. Select Contacts from the list of libraries, and then click Next.
  6. Select the Title, Street, District, State and Country fields, deselect every other field, and then click Next.
  7. Click Finish.

Design the form

Switch back to InfoPath Designer and Select the Address Datasource(this the one which is generated above)

manage

Choose Repeated table as shown in the Screen Shot below

RepeatedTable

Change the property of the textbox to read only for all the textboxes in the repeating table

ReadOnly

 

Add Name, Street, District, State and Country Fields to the Main Data Source.

MainDataSource

And Finally the forms looks like the below screen shot.

Desing

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

Code

 

(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

Step1

Step 2:

Add the below line of code at the bottom of the code mentioned above

xnEmployeeContacts.SelectSingleNode(“/my:myFields/my:Check”, ns).SetValue(“1”);

Step 3:

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.

Select Check field and click on Manage Rules Image1 . Now create a new rule with the condition if Check is Equal to 1.

Step3

On the run these actions select Query for data and select Address data source.

Step4

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!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s