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 1 and 2 and type the answer here:
  • Post
  • Hi "Question" - the only way I would know to do what you need would be submit to a custom web service that takes the data from the XML file and adds it to the list. Then all entries would come in under the same account...whatever the identity account is for the web application.

    Short of writing a custom web service for this, I don't know of a way to submit to a list anonymously. You may want to ping a SharePoint forum on this for ideas as I am not a SharePoint expert.

    Scott

  • I am new to InfoPath and SharePoint. I have been trying to submit data from InfoPath 2003 (***) to SharePoint list.

    01. My list GUID is in format {95EF64EA-0FC0-45F3-B00A-212CF8AEFEA6}

    02. My CAML is:

    <?xml version="1.0" encoding="UF-8"?>

    <Batch OnError="Continue" ListVersion="1" ViewName="{79FFC957-EE73-4653-868D-34D4CA6DC75}">

       <Method ID="1" Cmd="New">

           <Field Name='Title'></Field>

           <Field Name="ProgramName"></Field>

           <Field Name="PGM"></Field>

           <Field Name="PMT1"></Field>

           <Field Name="PMT2"></Field>

           <Field Name="PMT3"></Field>

           <Field Name="PMT4"></Field>

           <Field Name="PMT5"></Field>

           <Field Name="PMT6"></Field>

           <Field Name="PMT7"></Field>

           <Field Name="PDQOS"></Field>

           <Field Name="Platform"></Field>

           <Field Name="Folder"></Field>

           <Field Name="MS"></Field>

       </Method>

    </Batch>

    !!! Do I need to add <Field Name="ID"></Field> to my CALM file !!!

    03. I added a "Receive" type secondary data connection to my CAML

    04. I added a data source (receive) to the sharepoint list required.

    05. I added a "Submit" type of web service data connection to the "lists.asmx" web services

    (http://servername/sites/mysite/_vti_bin/lists.asmx); with the "UpdateListItems" method;

    Provide my list GUID to "listname" parameter; and select my CAML data source to "updates" parameter ("BATCH" and

    "XML subtree, including selected element")

    It is a simple form that just wants to submit to the list.

    When I preview the form - I can enter the values and submit. I then get NO response (No Error) but the new item was not

    added to the SharePoint list.

    Any Help is greatly appreciated.

    (***) I create the InfoPath 2003 form from InfoPath 2010 (using compatibable option). is there any differences from

    the sample that I should aware to correct my issue?

    my email is haichautrinh@yahoo.com

    Thanks

  • Hi Tricia,

    I know there are a lot of commments on this post but if you read through them, the behavior you are experiencing is addressed throughout this post. In short, there are a number of "failure" points where you can see this behavior:

    1) Make sure you are using the correct URL for the "lists.asmx" web service. For instance, if you list is located off the root like this: http://server/Lists/ListName then the submit URL would be like this: http://server/_vti_bin/Lists.asmx. However, if your list resides in a subsite like this: http://server/site/Lists/ListName then the web service URL would be like this: http://server/site/_vti_bin/Lists.asmx

    2) Make sure your "Add List Item Template" XML file contains the actual "SharePoint" column name. For instance, say you create a column in your list named: My Column but afterwards realize you don't want it to have a space. If you go back to your list and rename it without the space, the underlying SharePoint column name will *still* have the space and it will be encoded like this: My_x0020_Column. It is this name that needs to be in your Add List Item Template XML file. Here are the easiest steps to determine the actual SharePoint column names for your columns:

    - Create a new, blank InfoPath Form Template (XSN) - I create a new one just so you can throw it away when this is done.

    - Add a Receive data connection to your list AND be sure to include all the columns you have added to your Add List Item Template XML file

    - Create a new folder on your Desktop named: SourceFiles

    - From the File tab in InfoPath 2010, choose Publish and then select "Export Source Files"

    - Select the SourceFiles folder and click OK (you can now close completely out of this InfoPath form)

    - Navigate into the SourceFiles folder, right-click on "manifest.xsf", choose Open With and use Notepad to open this file

    - Do a Search in Notepad for this: <xsf:dataObjects>

    - Once you find the above node, if you look below it you will see your SharePoint list connection information. In these nodes, you will also see entries like this:

    <xsf:field internalName="Title" required="yes" type="Text"></xsf:field>

    - Pay particular attention to the "internalName" attribute - whatever is specified here for your column name is what MUST be in your Add List Item Template XML file. Make sure of the spelling AND capitalization.

    3) Once you have completed the above steps, if the update still does not work then I would suggest (make a copy of your Add List Item Template XML file) removing all but one of the fields you have in this file and see if you can get that one field to update correctly. If so, then continue to add them back until you find the one(s) that cause the process to no longer work. For instance, if any of your columns are a Date and Time column, then you have to have the value entered in a specific format.

    Hopefully some of this helps! The information above (along with other suggestions) are throughout this blog.

    Scott

  • I am getting this error message and cannot figure out how to fix it i followed all the directions above.

    Darren Weiss

    dweiss@nybloodcenter.com

    InfoPath cannot submit the form.

    Some rules were not applied.

    InfoPath cannot submit the form.

    An error occurred while the form was being submitted.

    The SOAP response indicates that an error occurred on the server:

    Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

    <detail><errorstring xmlns="schemas.microsoft.com/.../">

       List does not exist.

       The page you selected contains a list that does not exist.  It may have been deleted by another user.

       </errorstring><errorcode xmlns="schemas.microsoft.com/.../errorcod

  • Hi Darren,

    The first thing to check is that the URL to the "lists" web service is at the same "level" as your list. For instance, if your list is here:

    http://server/lists/YourList

    Then the web service URL for the lists web service would be:

    http://server/_vti_bin/lists.asmx

    However, if your list is on a lower subsite like this:

    http://server/site/lists/YourList

    Then the web service URL for the lists web service would be:

    http://server/site/_vti_bin/lists.asmx

    I would start by double-checking you have the correct "lists" web service URL. If it does not point to the same "level" (site) as where your list lives then you will get an error that the list cannot be found.

    Scott

  • Hi Scott,

    How will I save my infopath in offline mode. My req is that I need to fill the infopath as many times as I can when I am in offline mode, save it as xml and then submit them to the sharepoint.

  • Hi Heerachand,

    You will need code for this but it is really no different than any other offline InfoPath form. Your code will need to:

    - Check if the user is online or offline

    - If offline:

        - Submit the form (the entire XML file) to a local folder

        - When the user comes back online, your code will need to enumerate all the XML files in that folder and submit them individually to the list.

    - If online:

        - Submit to the list

    Scott

  • Hi Scott, I've adapted the steps above to update rows in a sharepoint list instead of adding new rows, and it works fine. However, I don't want users to actually see the repeating rows of data, I would rather set them 'behind the scenes.' In my rules I have tried to set the repeating fields, but so far it's updating all the 'field' rows in the repeating table to the same value even though I set my rule to update based on the @name.

    This is what I have in two separate rules:

    Set Field to 1 where @Name=ID

    Set Field to Reserved where @Name=Status

    This is what I get back:

    ID=Reserved

    Status=Reserved

    Any suggestions would be greatly appreciated.

  • Hi Matt,

    I know its a lot of comments on this post but if you look through them you will see that you need to use code. Rules cannot be used to set data in a repeating structure. At various points in the past, I have provided sample code for this process.

    Thanks,

    Scott

  • I am hitting two issues with this tutorial. The first is if I have the following CAML:

    <?xml version="1.0" encoding="UTF-8"?>

    <Batch OnError="Continue">

       <Method ID="1" Cmd="New">

    <FieldRef Name="LinkTitle"/>

       </Method>

    </Batch>

    The form will successfully write to the list, but it wont record the title that i provided. Instead, when navigating to the sharepoint list it says "(no title)".

    If I update the CAML to be <FieldRef Name="Title"/> I get the following error:

    Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

    <detail><errorstring xmlns="schemas.microsoft.com/.../">Cannot access a closed Stream.</errorstring></detail>

    I also get this error if I add any fields other than the LinkTitle one.

  • Hi Stacey,

    I am not sure I follow exactly what field you are trying to set - so let me describe what I just tested:

    - Created a new list called: SubmitToListTest

    - By default, the Title column that you get is: Title (linked to item with edit menu)

    - I added one additional column to the list called: Col1 (NOTE: the actual SharePoint name for this column is: _x0043_ol1 - I am not sure why SharePoint changed "Col1" to "_x0043_ol1" but it did.)

    - Setup my InfoPath Form Template as per the initial post and tested - result: the data submitted and appeared without issue. And in this case, the "Title" column that got populated is the one that is a "link" - meaning I can click on the title.

    If I am not following correctly, please accept my apologies.

    Scott

  • Hi Scott,

    Sorry for my delay. I was able to solve my issue (though I've hit a few others.) Basically, I had copied the field references from the code view of a sharepoint list view.. which gave me:

    <FieldRef Name="Title"/>

    Instead of:

    <Field Name="Title"/>

    (And boy do I feel silly!)

    I am stuck once again and having trouble finding resources online and have read over the comments on your blog. First let me provide some background information on what I am trying to do.

    I have two custom sharepoint lists - Event Details and Schedule. This is a 1 to many relationship, meaning 1 event to many schedule items. I want to be able to create and edit items for both lists in a single form.

    With that said, I am using Infopath & Sharepoint 2010, would prefer to keep it as a browser form and will not be able to use custom code.

    Do you have any good resources for me? I am able to get the new item code to work, but I need to also have update, delete and filter capabilities.

    Thanks in advance!

    Stacey

  • Hi Stacey,

    Glad you were able to get this working! So in regard to your follow-up questions...this is going to be a bit challenging...especially if you cannot use code. What you are going to have to do is implement a way to set the "Cmd" attribute in the list template XML file to the appropriate command (i.e. New for adding items, Update for editing items and Delete for deleting list items.) In addition, you will need an XML file for each list you are updating. So your button will need to update the appropriate XML file and then not only update the fields (nodes in the XML file) but also set the appopriate "Cmd" attribute as well.

    So the way that I would implement this would be as follows:

    - A "Section" type of control for the parent list fields with buttons to Add/Edit/Delete

    - A "Repeating Table" type of control for the child list items with an additional column for the Add/Edit/Delete buttons (this way these buttons appear in each row of the repeating table so it is easy to set the correct values)

    A few additional comments:

    - With the Update and Delete, you will need to set the ID node in the XML file so you update/delete the appropriate item in the list

    - If you allow a deletion of a parent item, you will probably need to implement logic to delete the corresponding child items as well.

    I have not tested this scenario myself so off the top of my head I don't know of any other "gotchas." :)

    Scott

  • THanks for the information Scott! Do you know of a way to set the ID in the CAML dynamically? How I'm envisioning this form is having the receive repeating table with a button that says "delete" and another that says "Update" so that you can modify each row. How do I send the ID to the CAML?

    Also, do you have any information around filtering the receive connection? I see query fields, and from reading over the InfoPath blog it sounds like you can use those fields to filter.. but I can't seem to figure out how.

    Thanks!

    Stacey

  • Hi Stacey,

    Sure - when you add a column to the repeating table to store the buttons, you get a button on each row. As such, when you click the button to set the CAML fields, the values come from the fields on the same row as the button. You will just need to include the ID column from your list.

    Now the issue you are going to run into is this: how do you actually set the appropriate fields in the CAML file from a button...and this is where it gets tricky. If you read through all the comments in this post you will see I have documented a couple of options for this:

    1) Hand-modify the manifest so you can specify through a subscript value which node gets which value.

    2) Use code behind the XSN to set the nodes in the CAML file

    So the problem with option #1 is this: that method will only work in an InfoPath client form. It is not supported for the browser. The issue with option #2 is that you now are adding code to your form template. This method will work for client or browser but you are adding code.

    Now, if you are using InfoPath 2010 *and* SharePoint 2010, then in many cases you can still publish your XSN directly to a library even with code! The caveats for this are: the "Microsoft SharePoint Foundation Sandboxed Code Service" must be running and the code that you are executing cannot access anything outside of the site collection. In this case, the code is just manipulating data with the XSN so the only other issue would be if the Microsoft SharePoint Foundation Sandboxed Code Service is running in your farm.

    Within the comments in this post I have provided a complete code sample for adding, updating and deleting list items so with the new Microsoft SharePoint Foundation Sandboxed Code Service feature of SharePoint 2010 this should be a viable option.

    I hope this helps!

    Scott

Page 26 of 37 (542 items) «2425262728»