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 6 and type the answer here:
  • Post
  • Ashley,

    When you click the Edit Form Code button it takes you to the code and, by default, it will look like this:

    public void CTRL1_5_Clicked(object sender, ClickedEventArgs e)

    {

       // Write your code here.

    }

    ** The name will probably not be CTRL1_5 but other than this, it should look like what is above.

    Where is shows "//Write your code here" - here is what you should have there:

    UpdateList("Update", e.Source.CreateNavigator());

    So now, the clicked event should look like this:

    public void CTRL1_5_Clicked(object sender, ClickedEventArgs e)

    {

        UpdateList("Update", e.Source.CreateNavigator());

    }

    Then just below the "closing brace" in the above procedure (the closing brace is the "}"), you need to have this entire procedure:

    public void UpdateList(string strOperation, XPathNavigator e)

    {

        //All the code from this procedure

    }

    Scott

  • thank you! it was only an error with one of the names. There are no more errors when I debug the code but now when I try to make an edit in the repeating table I get the following error:

    Object reference not set to an instance of an object.

      at Updated_Headcount.FormCode.UpdateList(String strOperation, XPathNavigator e)

      at Updated_Headcount.FormCode.UpdateList_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)

  • Hey Ashley,

    This is where you are going to need to know some code or this is going to be somewhat painful. Chances are the "XPATH" expressions in your code are not correct. But to find out why this fails you will need to debug your code. So you need to make sure you are building for Debug mode and not release then you can add a breakpoint in your code, start in debug mode and then step through the code to see exactly which line fails and why.

    Scott

  • Okay, thanks so much Scott for your help!

  • Hi,

    Thanks for your post but need your help.

    I am just trying to submit data to sharepoint list using webservice like you suggested but i can new item added in sharepoint list but it is blank.

    Plz help me.

  • Hi,

    Thanks for your post but need your help.

    I am just trying to submit data to sharepoint list using webservice like you suggested but i can new item added in sharepoint list but it is blank.

    Plz help me.

  • Hello,

    What does the SharePoint list look like after the InfoPath Form is submitted?  I would be nice to see a screen shot of how the table looks in the Columns of the list.  Thanks.

  • Hi,

    I am not able to attach any screenshot over here so if possible can me give me email address.My email id is kaushalbagrodia@yahoo.com

  • HI,

    I am using your code on 15th page which can be used for non repeating fields. But its not working correctly.

    Is there anyway i can add non repeating field value to sharepoint list without using code.

    Thanx

  • Hi Andy,

    Not sure I follow: the code can be modified to work with repeating or non-repeating fields. In addition, you can easily add data from non-repeating fields without using code... if I recall correctly, there are samples of this in the comments (it's been a while so I don't recall 100%) - but with a slight modification of the code (using an XpathNavigator object instead of an XpathNodeIterator object), it would work as well.

    Scott

  • Hi,

    This was your comment.

    Here are the new steps - and again these will only work using the InfoPath client. (NOTE: I would suggest you first create a new sample XSN to make sure this works and then implement this in your actual XSN.)

    This update will still follow the same basic steps outlined in this blog post except for step #7.

    - Complete steps #1 - #6 in the original post

    - For step #7, implement the following:

       o Right-click on the root “myFields” and choose Add

            * For Name, enter: gpContacts

            * For Type, select: Group

            * Click OK

     .........................

    - 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: Set a field’s value

    - Click the Select XPath button next to Field

       o From the Data Source dropdown box choose: Add List Item Template

       o Drill down, select the “Field” node and click OK

       o Click the “fx” button next to the Value field

    ......................

    .......

    - Navigate to the SourceFiles folder, right-click on manifest.xsf, select “Open with…” and open this with Notepad

    - Locate the following node in the file: <xsf:ruleSets>

    - Directly under this node, you will see the “sample” Rule that we created above as follows:

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field" expression="my:gpContacts/my:_Title"></xsf:assignmentAction>

    - Copy this entire line, scroll to the end of this line, hit Enter and paste this again

    - Repeat the above step three more times so you have a total of five lines that have the same information (the number of lines you have here correspond to the number of fields in your Add List Item Template.xml file)

    - Add an “index” to each “Field” node, like this:

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field[1]" expression="my:gpContacts/my:_Title"></xsf:assignmentAction>

    When you are done you the index number should be incremented for each "field" (i.e. [1], [2], etc.)

    - Modify the “expression” portion of each assignment to point to the appropriate node in your “man” data source:

    For each line, change: my:gpContacts/my:_Title to point to the correct node that we added (i.e. _Title, _FirstName, etc.)

    NOTE: It is very important that your “expression” nodes are in the same order as what you have in your Add List Item Template.xml file. In this sample, “Field[1]” is Title, “Field[2]” is FirstName, etc. Also – like all XML, capitalization must also match what you have in your data source.

  • So i am using this solution to submit nonrepeating fields to submit to sharepoint list. But i guess i am doing something wrong at this step:

    - Modify the “expression” portion of each assignment to point to the appropriate node in your “man” data source:

    For each line, change: my:gpContacts/my:_Title to point to the correct node that we added (i.e. _Title, _FirstName, etc.)

    So can you tell me exactly what would be changed expression line for _Title.

    I would appreciate your help sincerely.

    Also pasting the changed content of .xsf file below ...

    <xsf:ruleSet name="ruleSet_1">

    <xsf:rule caption="Rule 1" isEnabled="yes">

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field" expression="my:gpcontacts/my:_Title"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field[1]" expression="my:_Title"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field" expression="my:gpcontacts/my:_FirstName"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field[2]" expression="my:_FirstName"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field" expression="my:gpcontacts/my:_LastName"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field[3]" expression="my:_LastName"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field" expression="my:gpcontacts/my:_Email"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field[4]" expression="my:_Email"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field" expression="my:gpcontacts/my:_Phone"></xsf:assignmentAction>

    <xsf:assignmentAction targetField="xdXDocument:GetDOM("Add List Item Template")/Batch/Method/Field[5]" expression="my:_Phone"></xsf:assignmentAction>

    </xsf:rule>

    </xsf:ruleSet>

    </xsf:ruleSets>

  • First of all thank you scott i learnt a lot from this post,But i am stuck at one place is that i want to add multiple rows of repeating table at one submit button click, Can you help me?

    Big Thanx in Advance.

  • Actually, similar question as Scot on the multiple rows.

    I know that if I create my form in infopath and publish it as a template I could add custom code to handle creating a new node in the CAML xml. Then each row in a repeating table could be used to create a new update node.

    Still, I'd like to avoid having any attached code if at all possible.

  • Bill F: the only way to add rows to a repeating structure is through code - this cannot be done via a Rule.

    Scot: adding multiple rows using code is not specific to what this we're doing in this post. Just search the Internet for how to add rows to an InfoPath repeating table using C# and you should get plenty of information. Then just incorporate that into this solution. I may have some of that type of sample code in the various comments on this post. Have you checked?

    Scott

Page 33 of 36 (531 items) «3132333435»