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

    Have you looked at the Application Event Log or the Server logs to see if there are more details to the error? Is this a browser form? If so, you will probably need to change the logging options to Verbose for the Forms Services logging operations.

    Scott

  • How would you handle a repeating table/section with the SharePoint list?  

  • Hi ndiem,

    I have not tried this but just off the top of my head, you could use code to enumerate the nodes in your repeating table and execute the web service/CAML directly.

    I don't know that there is a way to do this without a custom code procedure.

    Scott

  • How would you write code to enumerate the nodes in a repeating table and then execute a web service?

    Can you point me to any samples or tutorials?

    Thanks.

  • Hi kaj,

    Here is some sample code I wrote to simply enumerate the existing contacts returned from the list and subsequently update those same contacts with the new values entered in the table. You could use this as a basis for how to accomplish what you need:

    public void CTRL26_5_Clicked(object sender, ClickedEventArgs e)

           {

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

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

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

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

               //myList.Credentials = new System.Net.NetworkCredential("user", "password", "domain");

               //Create a DataSource object for our list data source

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

               //Create a Navigator object so we can enumerate the list

               XPathNavigator xnContacts = dsContacts.CreateNavigator();

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

               XmlNamespaceManager ns = this.NamespaceManager;

               //Create a NodeIterator object to enumerate all of the existing contacts

               XPathNodeIterator xi = xnContacts.Select("/dfs:myFields/dfs:dataFields/dfs:MyContacts", ns);

               //Enumerate each existing contact and update the values in each record with the changed information

               while (xi.MoveNext())

               {

                   //Create XPathNavigator objects for each field from our list

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

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

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

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

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

                   XPathNavigator xnPhone = xi.Current.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");

                   string strOperation = "Update";

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

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

                   "<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

                   XmlNode node = myList.UpdateListItems("EAD1852F-6884-47B0-B928-BCB167698784", updates);

                   dsContacts.QueryConnection.Execute();

               }

           }

  • Excellent work Scott (and Matt).

    I am a beginner.  I got the demo working but have a couple of questions.

    I would like to essentially implement the solution you have done, except with two changes:

    1)  I would like the ability to customize the look of the form some more, which has been mentioned elsewhere in the thread, including by Matt.  So I assume there is nothing new in that regard and will continue monitoring this and other forums.

    2)  I would like to be able to do this with a document library.  Everything I find seems to be on how to submit the actual form to the library.  What I am attempting is a little bit different from the examples I have found- I want the user to be able to browse to a file to upload to the document library (just like in Sharepoint), to collect all of the other fields I need just as in your list example, and then click submit to create a new entry in the document library with the "browsed-to" file as the file in the document library.

    FYI- The reason for doing this instead of just the Sharepoint add, in our case, is so I can gain some more control (and provide choices from some different data sources) for their selections for the other columns that will ultimately describe the document.

    I hope that makes sense.  Thanks in advance.

    presack

  • Hi presack,

    Agreed - it would be nice to have a re-design of the form but I just have not had time to think about this much - sorry.

    Regarding your 2nd question - is your form designed for the browser for for the client?

    Scott

  • Thanks for the response, Scott.

    No problem on point one- that's what I figured.  I am not far enough along right now in my experience (thus the novice post) to develop a way myself yet.

    I am currently planning on using the browser-enabled form, but using the client is an option in our environment.  All users have InfoPath 2003 SP1.

    Thanks,

    presack

  • Hi presack,

    If your preference is a browser form, you may want to look at using a custom ASPX page with the XmlFormView control. This way you can easily provide an option to allow your users to "browse" for a file to upload.

    Then you could create a web service on your SharePoint server that uses the SharePoint Object Model functionality to grab the selected file and upload it to the appropriate document library. I don't have any sample code on this but you should be able to search around and find something - maybe on a SharePoint BLOG.

    Here is a link to using the XmlFormView control:

    http://msdn2.microsoft.com/en-us/library/aa701078.aspx

    Scott

  • Thanks again.  That looks like a potentially workable solution.  Another article I found that may be of interest to people in this thread (although it is not an Infopath solution- sorry!) is below:

    http://msdn2.microsoft.com/en-us/library/ms916819.aspx

    It provides the ability to customize Sharepoint List forms (both the input and the display).  It looks to me like it has some drawbacks, but I'm not in a position right now to have the time to implement it.  I just thought it was worthy of note.

    presack

  • Hi,

    I try to complete the “Submitting to a SharePoint List” but I have the same problem that read in your comments “When I’m submitting my form I recieve a message that tells me that the data has been posted successfully. But of course, no item in the list.”. I checked everything again and again but the problem remains. I try the example with Contact List of MOSS (Parent example from Matt Faus) and works (needs a simple change on GUID and Submit button). I try to create my own list (in same site and area with contact list) and run the example but the result was dishearting. I check the settings of two lists (mine and contacts)no difference except field “Title” that included only in my list. Did anybody knows the reason?

    Giannis

  • Hi Scott,

    in an template example that you given at June 18 to kaj you use

    "server.Lists myList = new SubmitToSharePointList.server.Lists();"

    I try it but I cann't find the assembly reference. An idea please.

    thanks for your help.

    Giannis

  • Hey all,

    I've followed all of the steps in this example and am getting everything to work perfectly except:

    I need to have this form be browser-enabled so that clients without InfoPath can fill out the form and thus populate a SharePoint list without ever being directed to the List's actual add item page.

    When starting the template I enabled browser enabled features only, so I figured this wouldn't be a problem, but it seems to be.  The design checker tells me "Invalid form Template" and the detail box says "An unexpected error has occured while verifying the form template."

    I'm wondering if this might be a problem with how Forms Services is configured on the server, or any other suggestions you might have.  

    Thanks,

    Matt Callaway

  • Hello,

    How can I create initially automatic file name when the user clicks on "submit" button (at web InfoPath on the ribbon area)?

    Tools-> submit options-> to SPS '07

    Sample: "file_" + now() + userName

    Thank for original post.

    Ishay

  • Hello,

    may be you can give me a hint how to do this:

    I can insert new items to the list as long as I don't use a field that contains a Person/Group-Field. I think, I need a GUID or something like this for the person-Field - but where can I get the information from?

    I tested with a GIUD-like value I got from the webservice:

    userprofileservice.asmx - GetUserProfileByName. But this doesn't work.

    Any idea what's correct?

Page 4 of 36 (537 items) «23456»