Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Submitting to a SharePoint List

Submitting to a SharePoint List

Rate This

As you know, submitting to a Microsoft Windows SharePoint form library from an InfoPath form is quite simple: just add a “submit” type of data connection to InfoPath and away you go. However, submitting to a SharePoint List is another matter. In this post, we will take a look at the steps necessary to enable this functionality.

Step 1: Create the list

1. Create a new custom list named: MyContacts
2. From the Settings button choose List Settings
3. Click Create Column
4. Add the following columns/data types:

  • FirstName, Single line of text
  • LastName, Single line of text
  • Email, Single line of text
  • Phone, Single line of text

The next step is to create a Collaborative Application Markup Language (CAML) template that contains the same XML nodes as the columns we added to our list – this will be used as a secondary data connection in the InfoPath Form Template.

Step 2: Create the CAML template

1. Launch Notepad (or any text editor)
2. Copy and paste the following code to the Notepad document:

<?xml version="1.0" encoding="UTF-8"?>
<Batch OnError="Continue">
    <Method ID="1" Cmd="New">
        <Field Name='Title'></Field>
        <Field Name="FirstName"></Field>
        <Field Name="LastName"></Field>
        <Field Name="Email"></Field>
        <Field Name="Phone"></Field>
    </Method>
</Batch>

3. Save this as: Add List Item Template.xml

 

Step 3: Create the InfoPath Form Template

1. Create a new, blank, browser-compatible form template
2. Add a “Receive” type secondary data connection to the Add List Item Template.xml file created in Step #2. (Make sure the options “Include the data as a resource file in the form template” and “Automatically retrieve data when the form is opened” are enabled.)
3. Add a “Receive” type secondary data connection to the “MyContacts” SharePoint List created in Step #1 and make sure the option “Automatically retrieve data when the form is opened” is enabled.
NOTE: Be sure to include the “Title” field along with the custom fields we created.

Each SharePoint list is created with a unique identifier called a GUID. In order to submit items to a SharePoint list, you must know the unique GUID for that list. These next steps will enable you to identify the GUID for your “MyContacts” list.

Step 4: Identify the list GUID

1. Open a browser and navigate to the MyContacts list
2. From the Settings menu choose List Settings
3. In the browser’s address bar you will see the URL appended with “List=”

 

4. Copy everything after the equals sign and paste this into a text editor as we will need this value later on.

NOTE: We will need the list GUID to be formatted as “{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}”. If your GUID appears as above with the hyphens and braces “escaped” (i.e. %7B for the braces and %2D for the hyphens) then you will need to replace the escaped characters with the braces and hyphens so when we use this GUID later, it will appear as: {1E76517B-2C36-4473-A420-A737D98589BC}

Step 5: Add controls to the InfoPath Form Template

1. Open the Data Source Task Pane
2. From the Data Source dropdown box choose the “MyContacts” connection

3. Right-click on “MyContacts” and choose Repeating Table – this will add a repeating table bound to that connection to your View
4. From the Data Source dropdown box choose Main
5. Add a text box to the “myFields” node named: ListName and set the Default Value property to the GUID value of your list – we will use this node to specify the list when we perform the Submit operation

 

To update the SharePoint list we will be using the “UpdateListItems” method of the SharePoint “lists.asmx” web service. This web method requires a couple of parameters (the list name and what gets updated) – now that we have added the “ListName” node which contains the GUID and we have the Add List Item Template XML data connection which describes our data structure we have all the necessary information to add the “UpdateListItems” web method!

Step 6: Add the “lists.asmx” web service

1. Add a new “Submit” type of web service data connection to the “lists.asmx” web service – this is typically located at: http://servername/_vti_bin/lists.asmx
2. Select the “UpdateListItems” operation
3. Double-click the “listname” parameter and assign the “my:ListName” node as the value

4. Double-click the “updates” parameter, select the “Add list Item Parameter” data source, select “Batch” and click OK
5. For the “Include” option select “XML subtree, including selected element”

6. Complete the Data Connection Wizard

Now that we have all the connections that are required, we now need to add the “submit” functionality to the form template.

Step 7: Add Submit functionality

1. Display the Data Source Task Pane
2. From the Data Source dropdown box choose the Add List Item Template data connection
3. Drill down through the data source, right-click on the Field node and choose Repeating Table

 

We need to change the text box control in the “Name” column to Read-only – if these values were to be changed it would affect the CAML and the submit would fail.
• Double-click on the text box in the Name column of the Repeating Table
• Select the Display tab
• Enable the Read-only property and click OK
• Add a new Button control to the View
• Double-click the button to display the Properties
• Click the Submit Options button
• Enable the option “Allow users to submit this form”
• Enable the option “Perform custom action using Rules” and click the Rules button
• Click the Add button
• Click the Add Action button
• From the Action dropdown box choose “Submit using a data connection”, select the “Web Service Submit” data connection and click OK
• Click the Add Action button
• From the Action dropdown box choose “Query using a data connection”, select the “MyContacts” data connection and click OK (this will automatically refresh the list in InfoPath so you can see the newly added record)

• Click OK until you are back to your form

NOTE: If you want to automatically clear out the submitted values, add another action to this Rule to “Set a field’s value” and the only option you need to specify is the “Field” option – select the “Field” node from the “Add List Item Template” data connection and then simply leave the Value option blank.

Step 8: Test!

1. Click the Preview button to preview the form
2. Fill in values for each of the fields: Title, First Name, Last Name, E-mail and Phone

3. Click the Submit button – the contact information should be submitted successfully and the “MyContacts” list automatically updated!

A special thanks to Matt Faus of InfoPathDev for for initially posting this solution!

Scott Heim
Support Engineer

Leave a Comment
  • Please add 5 and 7 and type the answer here:
  • Post
  • Hi Scott,

    Just to check if I saw this correctly. In Step 6-4."Double-click the “updates” parameter, select the “Add list Item Parameter” data source, select “Batch” and click OK" - Do you mean Add list Item Template?

    I don't think I created a data connection with that name.

    furthermore, when I submit my form, items are actually being added to the SP list. The only problem is that it's blank. Title is '(no title)' and other columns are empty. Would you happen to know where I got it wrong?

    Thanks in advance!

  • Hi Essined,

    My apologies - yes, that should have been "Add List Item Template." But in short, you will use whatever connection you created for that XML file. In regard to why they are appearing blank - it sounds as if the values in your form are not being mapped back to the nodes in that XML file.

    Scott

  • Her is the code I use

    {

               // Write your code here. This is line 35

                           XmlNamespaceManager ns = this.NamespaceManager;

               //Create a DataSource object for the Add List Item Template data source

               DataSource dsAddListItem = this.DataSources["RecieveCompanyArchiveBatch"];

               //Create an XPathNavigator object for that data source

               XPathNavigator xnAddListItem = dsAddListItem.CreateNavigator();

               //Create an XPathNavigator object for the main DOM

               XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

               //Create XPathNavigator objects for each of our fields in the main DOM

               XPathNavigator xnComName = xnDoc.SelectSingleNode("/my:myFields/my:Group1/my:ComName", ns);

               XPathNavigator xnComAddress = xnDoc.SelectSingleNode("/my:myFields/my:Group1/my:ComAddress", ns);

               XPathNavigator xnComPhone = xnDoc.SelectSingleNode("/my:myFields/my:Group1/my:ComPhone", ns);

               XPathNavigator xnComMail = xnDoc.SelectSingleNode("/my:myFields/my:Group1/my:ComMail", ns);

               XPathNavigator xnTitle = xnDoc.SelectSingleNode("/my:myFields/my:Group1/my:Title", ns);

               XPathNavigator xnPersonName = xnDoc.SelectSingleNode("/my:myFields/my:Group1/my:PersonName", ns);

               XPathNavigator xnPersonPhone = xnDoc.SelectSingleNode("/my:myFields/my:Group1/my:PersonPhone", ns);

               XPathNavigator xnPersonMail = xnDoc.SelectSingleNode("/my:myFields/my:Group1/my:PersonMail", ns);

               //Set the values of each field in the companyXML data source to the values from the main DOM

               xnAddListItem.SelectSingleNode("/Batch/Method/Field[1]",ns).SetValue(xnComName.Value) ;

               xnAddListItem.SelectSingleNode("/Batch/Method/Field[2]",ns).SetValue(xnComAddress.InnerXml);

               xnAddListItem.SelectSingleNode("/Batch/Method/Field[3]",ns).SetValue(xnComPhone.Value);

               xnAddListItem.SelectSingleNode("/Batch/Method/Field[4]",ns).SetValue(xnComMail.Value);

               xnAddListItem.SelectSingleNode("/Batch/Method/Field[5]", ns).SetValue(xnTitle.Value);

               xnAddListItem.SelectSingleNode("/Batch/Method/Field[6]", ns).SetValue(xnPersonName.Value);

               xnAddListItem.SelectSingleNode("/Batch/Method/Field[7]", ns).SetValue(xnPersonPhone.Value);

               xnAddListItem.SelectSingleNode("/Batch/Method/Field[8]", ns).SetValue(xnPersonMail.Value);

               //Submit the data

               this.DataConnections["WebServiceSubmit"].Execute();

           }

  • This comment is related to the code in the above comment.

    Hmm My first post dissipered my magic, so I will try again.

    When I try to submit I get the following error message:

    System.NullReferenceException

    Object reference not set to an instance of an object.

      at CustomerArchive_ver2.FormCode.CTRL17_5_Clicked(Object sender, ClickedEventArgs e)

      at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)

      at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

    I have one section with 1 Rich textbox, 5 textboxes, and 2 comboboxes.

    The first combobox is populated from a sharepoint recieved connection(no  filter), and the second one with the first combobox as a filter.

    The sharepoint list names, are the excact same ones as in the form, and the xml fil( checked for spelling errors etc.).

    Where have I made an error???

    Troels

  • Hi Troels,

    Unfortunately there is no way of telling from just your code. You will need to run the form in "debug" mode so you can step through it and see what object is null.

    Scott

  • Is it possible to update a list from an Infopath form that is submitted to a document library?  So you are adding data to the document library and to the lis.

  • Hi Ron,

    Sure - just add another "Submit" connection to your SharePoint library and then execute this connection along with the one to send the data to the list.

    Scott

  • Hi Scott,

    I have tried the above steps and it worked correctly for me Thanks. However I need to submit customised form ( with 3 fields of single line of text, 2 drop down list values and two date field values) can you please help with the steps, I am new to infopath. Thanks.

  • I mean instead of a repeating table with same field types , I need a form with 3 single line text fields, 2 with dropdown and 1 with date. Once user selects all these and fills the infopath form, this should be saved in the share point list on click of Submit button on the form. How do i achieve this.

  • Hi Heerachand,

    This has been covered a number of times in this blog. I know there are a lot of comments but take a look through them as you will find complete steps, sample code, etc. for doing exactly what you ask.

    Scott

  • I found the answer to the /Batch question...

  • Hi Scott,

    I created the infopath form with drop downs and rich text boxes exactly like you mentioned in Page 13. Howevr there are some errors. Could you please provide a similar blog without repeating table but with fields like rich text box, drop downs, date etc submittion of infopath to sharepoint list. It would be really helpful as many of the previous comments from users require the solution for this. Thanks.

  • easy to see... about your tutorial... very clear step by step http://www.pembolang.com

  • Hi Scott,

    I know I'm pretty late to the party on this thread but I was hoping you still may be able to help:

    I'm hoping to use the idea behind this walkthrough to create a SharePoint Form Library form that submits a section of it's data to a separate list.

    I'm building a purchase order form and need to be able to submit the item details to a specific list for reporting purposes. I need to be able to have a repeating table that allows users to add new line items but the function is grayed out in the repeating table created here.

    I have tried wrapping the repeating table in a repeating section but that just duplicates the same data. I've been trying to find a solution to this for a week now, trying many hideous work arounds, and your walkthough has gotten me so close to an elegant solution so your help would be invaluable.

    Thanks either way,

    Oz

    (P.S - I'm running Infopath 2013 and SharePoint 2010, I'm hoping that doesn't make too much of a difference)

  • Hi CLockeWork,

    Are you saying that you added a Repeating Table (or Section) to your Form Template and when you attempt to add a new row, that option is grayed out? If so, how did you design your Form Template? Did you start by launching InfoPath and choosing Blank Form or are you customizing a SharePoint list?

    Scott

Page 29 of 36 (530 items) «2728293031»