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 4 and 1 and type the answer here:
  • Post
  • Hi everyone,

    Sorry for the delay in responding - I have been out of the office for the last week.

    Doubter13 - glad to hear you have resolved the issue! For everyone else, a couple of points to check:

    - If you have renamed the "Title" field to be your own name, the CAML query will still need to reference that field as "Title."

    - Check the Application Event log and see if there are any errors or warnings when you test this process - we could be hitting a permissions issue based on the Application Pool Identity Account being used in the Application Pool for your site

    Scott

  • - hi, Scott,  doubter13

    I try checked GUID for my list. They are correct but  no items add to my list. Please, help me. Thanks

  • Hi doubter13,

    I thought you had this resolved? Have you renamed the "Title" field in your list? Are there any errors or warnings in the Application Event log or the server log?

    Scott

  • Hi guys,

    Could you please help David (galetarconsulting.com) out, he posted a desperate message on my blog because I linked to this article, but he didn't know how to drop a comment here, or to get in contact with you all (I guess there's a need to register and login?). Anyway, here are his questions:

    In section #3, it says:

    "1. Create a new, blank, browser-compatible form template"

    - What does it mean by this?

    - How do I do this?

    In the same section it says:

    "2. Add a “Receive” type secondary data connection to the Add List Item Template.xml file created in Step #2.", and "3. Add a “Receive” type secondary data connection to the “MyContacts” SharePoint List created in Step #1"

    - What does it mean by this?

    - How do I do this?

    Lastly, I need to place this form on the companies "Public" portion of the website (for anyone to fill out), while I collect the data in a "Private" part of the website, or the "Sharepoint" part of it.

    PLEASE HELP- - - I WOULD REALLY APPRECIATE IT!!!!!!!!!

  • Hi,

    1. Create a new, blank browser-compatible form template

        - Launch InfoPath

        - Click Design a Form Template

        - Choose Blank and enable the option: "Enable browser-compatible features only"

    2. You add data connections to an InfoPath Form Template by selecting Data Connections from the Tools menu and clicking Add. There you can choose whether the connection type is Submit or Receive

    3. This is going to be challenging as I am assuming the "public" site is going to allow Anonymous Access. If so, then SharePoint security is designed to *not* allow any submissions to a library or list with Anonymous access enabled. As such, this process will change as the data will need to be submitted to a custom web service that you create to take the XML and create the list item for you.

    Scott

  • I got items in my list (used file XML and GUID of my list). But I not add item in my list (used web service of sharepoint).

    Please, help me.

  • Hi Cidras,

    If none of the other suggestions in these comments have been of benefit, then the best option would probably be a support case with Microsoft Product Support Services so this can be looked into further.

    Scott

  • yeah, I added item in my list. Thank you, Scott.

  • Am I missing someting?

    Shouldnt we just add Lists.asmx as the datasource and pass the SPquery? Why do we have to do CAML?

  • I everybody!

    I'm a newbi on both Sharepoint and Info path.

    I have a question.

    In the xml template, is it possible to refer a field that in the sharepoint it's a Choice (menu to choose from), that is acombo-box.

    If so, than how?

    Thanks in advance for the attention.

  • hi fbrum,

    Hope this solves your problem :)

    i have added a combo box name folderName and another text box named txtFolderName.

    Whenever the selection changes for the combo box i am reading the value into the text box.

    i have added manged code handler to the changed event of combo box ... (please note you will require VSTA installed onyour pc in order to add a .net code to your Infopath form)

    public void folderName_Changed(object sender, XmlEventArgs e)

           {

               // Get reference to the root node.

               XPathNavigator root = this.MainDataSource.CreateNavigator();

               //Get the reference to the text box.

               XPathNavigator text = root.SelectSingleNode("/my:myFields/my:txtFolderName", this.NamespaceManager);

               //Get the selected value into text box.

               text.SetValue(root.SelectSingleNode("/my:myFields/my:folderName", this.NamespaceManager).Value);

    }

    combo box is prepopulated by reading a list items in my sharepoint server....

    just go through the code,  its simple XML reading nothign else...

    revert back to me if you need any info :)

    hope this is what you were looking for...

    Regards,

    Sudhir

  • Thanks!

    That does clear things out. :)

  • Hi,

    Is it possible to modify the procedure to submit the entire form to a forms library?

    I know you can submit directly to a forms library, but we have a scenario with anonymous users and since we can not grant anonymous users write priviliges to libraries we need a method of submitting via a web service.

    Cheers

    apersson

  • Hi apersson,

    Here is a "sample" web service method for creating a new InfoPath XML file in a SharePoint library. To use this, you would:

    - Create a new Submit data connection in InfoPath

    - Point the "fileName" parameter to a node in your data structure (obviously you could hard code this as well)

    - For the "fileContents" parameter you will choose "Entire form" and then enable the option: "Submit data as string"

    [WebMethod]

       public void CreateInfoPathDocument(string fileName, string fileContents)

       {

           try

           {

               string pathFolder = "SubmitViaWebService"; //Where the form will be created

               SPSite site = new SPSite("http://server");

               SPWeb web = site.OpenWeb();

               SPFolder folder = web.Folders[pathFolder];

               if (fileName.IndexOf(".xml") == -1)

                   fileName += ".xml";

               string fileUrl = folder + "/" + fileName;

               System.Text.Encoding e = System.Text.Encoding.UTF8;

               byte[] b = e.GetBytes(fileContents);

               SPFile file = folder.Files.Add(fileUrl, b, true);

           }

           catch (System.Exception ee)

           {

               throw new ApplicationException(ee.Message);

           }

       }

  • Thanks Scott,

    Thank you for your fast reply.

    So this can't be done without coding?

    Cheers

    apersson

Page 2 of 37 (550 items) 12345»