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 Deniz,

    Chances are this is a login/permissions issue. When you use the InfoPath client you are coming in as that logged on user; however, when using the browser you will be trying to create the form as the identity specified in the Application Pool for your site.

    Have you taken a look at your server logs? Reproduce the error, then open the latest server log, scroll to the bottom and then do a search "up" for Forms Services - you will probably find an error regarding authorization, permissions, etc.

    Scott

  • Regarding the "Invalid property name", "Duplicate property name namespace in file schema.xsd" error messages, I have got over them by removing a "<xs:anyAttribute namespace="http://www.w3.org/XML/1998/namespace" processContents="lax"/>" reference that I found in the schema.xsd file.

    After you remove this reference, do not forget to "Convert Main Data Source" with the new schema file and check that your data source is intact.

  • Hi there,

    This post has helped me a lot, but I can't figure out how to retrieve and update a specific list item. I need to be able to click a link in a sharepoint item list, that will open the Infopath form used to edit that item and do the necessary processing, and then save the changes to that specific item.

    It sounds like it should be easy enough!

    Thanks,

    Garth

  • Hi Garth,

    Unfortunately it is not as easy as it may seem. The only way I have found to do this is to custom code because you need to be able to modify the CAML query to tell it what type of edit you are doing (i.e. New, Delete, etc.) and then for which item in the list.

    Using the steps in this sample, I have modified the table structure returned from the list to add a new column. This column simply holds a button that would be used to "update" the data in that row. Here is some sample code that I have used to accomplish this:

    - On the click event of this button, I have just this code:

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

    - Here is the "UpdateList" procedure:

    public void UpdateList(string strOperation, XPathNavigator e)

           {

               //Create a new instance of our "Lists" web service

               serverName.Lists myList = new serverName.Lists();

               //Pass the logged on users credentials to the web service

               myList.Credentials = System.Net.CredentialCache.DefaultCredentials;

               //Create a DataSource object for our list data source

               DataSource dsContacts = this.DataSources["MyContacts"];

               //Create a NamespaceManager object so we can use the various namespaces on our XPATH expressions

               XmlNamespaceManager ns = this.NamespaceManager;

               //Create XPathNavigator objects for each field from our list

               XPathNavigator xnID = e.SelectSingleNode("@ID", ns);

               XPathNavigator xnFirstName = e.SelectSingleNode("@FirstName", ns);

               XPathNavigator xnLastName = e.SelectSingleNode("@LastName", ns);

               XPathNavigator xnTitle = e.SelectSingleNode("@Title", ns);

               XPathNavigator xnEmail = e.SelectSingleNode("@Email", ns);

               XPathNavigator xnPhone = e.SelectSingleNode("@Phone", ns);

               //Create a new XmlDocument to use for specifying the Update CAML query

               XmlDocument doc = new XmlDocument();

               //Create the needed update element

               System.Xml.XmlElement updates = doc.CreateElement("Batch");

               //Create the CAML query passing in the value of each of our fields from the form

               if (strOperation == "Delete")

               {

                   updates.InnerXml = string.Format(@"<Method ID='1' Cmd='Delete'>" +

                   "<Field Name='ID'>" + xnID.Value + "</Field>" + "</Method>");

               }

               else

               {

                   //Create the CAML query passing in the value of each of our fields from the form

                   updates.InnerXml = string.Format(@"<Method ID='1' Cmd='Update'>" +

                   "<Field Name='ID'>" + xnID.Value + "</Field>" +

                   "<Field Name='FirstName'>" + xnFirstName.Value + "</Field>" +

                   "<Field Name='LastName'>" + xnLastName.Value + "</Field>" +

                   "<Field Name='Title'>" + xnTitle.Value + "</Field>" +

                   "<Field Name='Phone'>" + xnPhone.Value + "</Field>" +

                   "<Field Name='Email'>" + xnEmail.Value + "</Field>" + "</Method>");

               }

               //Update the list

               XPathNavigator xnListGUID = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:ListName", ns);

               XmlNode node = myList.UpdateListItems(xnListGUID.Value, updates);

               //Requery the data source to reflect the updates

               dsContacts.QueryConnection.Execute();

           }

    As you can see, this code can also handle a "Delete" submission as well.

    Scott

  • Hi Scott,

    Thanks for the reply! How do I open an Infopath form, from a Sharepoint list item (e.g. by clicking on an icon)? I assume I can put the form into a Sharepoint webpart? And, is it possible to pass a parameter to that form, that I can use in one of it's fields?

    Thanks,

    Garth

  • Hi Garth,

    Not sure I follow the first question...are you asking how to click on a SharePoint list item and that will open an InfoPath form? If so, I am not sure how you would accomplish this - a Content Query Part may be the answer.

    In regard to the parameters question, take a look at this blog post:

    http://blogs.msdn.com/infopath/archive/2007/02/26/passing-data-into-a-form-input-parameters.aspx

    Scott

  • Hi Scott,

    Perhaps I'm approaching my problem in the wrong way. Basically, I've got an employee list that shows where employees are, when they left and when they will be back. An employee needs to be able to click on an icon in their line that will open a form that allows them to select a location and a time of return and this must also start a workflow. Currently I am doing this using content types - the employee clicks on the edit icon that opens a content type that allows them to either select a location with a specific time, or to enter a location and return time. This was easy to and works, but it does not give me the flexibility to do all the processing that I need to (the processing that I need to do is very basic, but I find have to use multiple calculated fields rather than a few lines of code). That's why I was thinking of opening an infopath form from the line. But maybe you could suggest another way?

    The problem is that,although I am a developer, I don't know much about web development, Sharepoint or .net. Is there a way to put code on the sharepoint fields, like I could put code on SQL field? I feel a bit frustrated because I know the logic I need to apply, but I don't know where to put it!

    Thanks,

    Garth

  • Hi Garth,

    Unfortunately, I am not a workflow expert. In fact, with the group I am with (InfoPath support) we see very little of the workflow issues as they are primarily handled by our SharePoint folks (at least from an Office workflow perspective.)

    I don't mean to dance around your question but I don't want to steer you in the wrong direction. Have you seen the SharePoint/Workflow blog: http://blogs.msdn.com/sharepoint/archive/tags/Workflow/default.aspx ?

    You may want to post here and see if you can get some better direction.

    Now, what I am wondering is this: you mentioned that you are currently doing this with "Content Types." You can certainly publish an InfoPath Form Template (XSN) as a Site Content Type as well. Without knowing exactly how you are launching the content types, I wonder if this would provide any benefit? With this being an InfoPath Form Template simply published as a Content Type you can certainly manipulate the data the same way as if you published the form as any other type.

    Scott

  • I am new to InfoPath.  I tried this and it worked perfectly.  Can you elaborate on your response dated in April about integrating a dropdown column in a sharepoint list with infopath? It would be great if you could list the key steps.  Thanks!

  • Hi mpang,

    What I was referring to in my response was adding a secondary data connection in your InfoPath Form Template (the XSN file) that was a connection to a SharePoint list. You can then use that connection as the source of data for your dropdown box in your InfoPath file.

    However, if what you are asking is how to consume a "Choice" type of column from SharePoint in your InfoPath Form Template there is no direct way for this in InfoPath.

    Scott

  • Hi jrk555,

    Here is sample code that I just tested that works for me - obviously I am just hard-coding some values but try this and see if it works for you. This is the code I have in the click event of a button:

    //Create a new instance of our "Lists" web service

    server_Lists.Lists myList = new SubmitToSharePointList.server_Lists.Lists();

    //Pass the logged on users credentials to the web service

    myList.Credentials = System.Net.CredentialCache.DefaultCredentials;

    //Create a DataSource object for our list data source

    DataSource dsContacts = this.DataSources["MyContacts"];

    //Create a NamespaceManager object so we can use the various namespaces on our XPATH expressions

    XmlNamespaceManager ns = this.NamespaceManager;

    //Create a new XmlDocument to use for specifying the Update CAML query

    XmlDocument doc = new XmlDocument();

    //Create the needed update element

    System.Xml.XmlElement updates = doc.CreateElement("Batch");

    //Create the CAML query passing in values

    updates.InnerXml = string.Format(@"<Method ID='1' Cmd='New'>" +

    "<Field Name='FirstName'>ABC</Field>" +

    "<Field Name='LastName'>DEF</Field>" +

    "<Field Name='Title'>EE</Field>" +

    "<Field Name='Phone'>980-555-1212</Field>" +

    "<Field Name='Email'>abc@hotmail.com</Field>" + "</Method>");

    //Update the list

    XPathNavigator xnListGUID = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:ListName", ns);

    XmlNode node = myList.UpdateListItems(xnListGUID.Value, updates);

    //Requery the data source to reflect the updates

    dsContacts.QueryConnection.Execute();

    I would try this with a new form and even go so far as to hard code the field values and the list GUID - this way you can take all variables out of the picture and see if you can get this to work.

    Scott

  • Hi Scott,

    Thanks for taking time to check on it.

    I resolved the issue i am having. When we add a web reference to the lists web service, we need to include ?wsdl at the end. So it has to be something like http://server/site/_vti_bin/lists.asmx?WSDL.

    I didnot include that initially. I included that now and its working fine.

    Appreciate your help and thanks for posting this article.

    -jrk555

  • Hi jrk555,

    Excellent! Glad you have this working...

    Scott

  • Hi Folks,

    I have followed the exact steps as mentioned above but i am getting the message "The form was submitted sucessfully" but actually the item is not added into the list. I have ensured the correct GUID. I can see that many people have faced the same problem so hoping to get the exact solution of this problem...

    Hope to get the response soon.

    Thanks and Regards,

    S

  • Hi Guys,

    Could anybody reply me about my problem? I will appreciate if someone could. I am stuck with that as i am not able to inserting item into list even after following the above steps...

    Thanks a lot in advance...

    My mail id is software.sanjay@gmail.com

    Thanks,

    Sanjay.

Page 6 of 36 (535 items) «45678»