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

    My apologies for the delay in responding.

    For Giannis:

    - Make sure you are using the appropriate web for the "Lists.asmx" web service. For instance, if your List is at: http://server/Lists then you would use something like http://server/_vti_bin/lists.asmx. However, if your List is at: http://server/site/Lists then you would want to use something like this: http://server/site/_vti_bin/lists.asmx

    - Regarding the "Assembly reference" you are asking about - make sure you are developing on your SharePoint server box

    For Matt Callaway:

    - Have you been able to resolve this issue? When I developed this sample, it was with browser-compatibility enabled so this should work without issue. Let me know if you are still experiencing a problem.

    For Ishay:

    - You will want to use the "concat" function to concatentate text, functions and/or field names.

    For UKaren:

    - The only way I have been able to accomplish this (adding a new item with a Person/Group field) is to use code to insert the new item. I tested this with the "Tasks" list and was able to use the Contact Selector control on the form to allow the user to select a person. I then use a custom .NET web service to open the appropriate "web", create an "SPUser" object and return the "ID" of that user. This is what I finally use to set the "AssignedTo" field.

    Thanks everyone for your patience!

    Scott

  • Hello Scott,

    My scenario is:

    After I Open a web infopath form and complete all input fields then I submit the new form.

    now the form is save automatic in sps list

    _________________

    How can I Generate a unique identifier  prameter when i create new item on infopath automatically ?

    (mynane + unique_id() -> save automatically into sps list)

    T U

    IshayB

  • Hi Ishay,

    There are a number of ways to create a unique ID:

    - Use a data connection to a database that increments a number and returns that to you

    - Use a web service that does something similar, etc.

    There is not a built-in way to get the unique identifier from SharePoint since this does not get assigned until after the form has been submitted.

    Probably the easiest method is to either use a web service that gets the next value from a database or, if you are already using code in your form, you could generate a GUID value and add this to your form name.

    Scott

  • Hi Scott,

    thanks for your answer. It works fine.

    It helped me to find the following:

    Use Webservice UserGroup.asmx - GetUserInfo

    The result contains an ID and Name-Node.

    Concat them to a string like ID;#Name. This is what you have to paste to the field you want to update. This works as well for groups. Use GetGroupInfo instead.

    Another problem:

    The name of the column "Assigned To" in the tasklist has a blank in its name. I haven't found a way to update columns with a blank in its name (whatever datatype they have). Any idea how to overcome this?

    There are lots of columns coming from Microsoft with blanks and I don't want to customize them every time...

  • Hi UKaren,

    I am not sure I follow what you mean by a "blank" in its name. How are you determining this? When I go to my Tasks list and click New, I see the AssignedTo field but when I click in that field I don't have a blank.

    Sorry if I am missing something obvious.

    Scott

  • Hallo Scott,

    sorry - for my bad english. on all sharepoint-server I have access to the column name of the AssignedTo field is written as "Assigned To" - between Assigned and To there is a character (Ascii 32). That's what I call "a blank".

  • Hi UKaren,

    Thank you for the clarification - now I understand!

    In actuality, when you are setting the "Assigned To" column to a value, the underlying column name is "AssignedTo". This appears to be true of most, if not all, of the column names. Even though they show spaces, the actual names do not contain a space.

    For instance, "Start Date" is actually "StartDate", "Workflow Name" is "WorkflowName".

    If you ping a SharePoint Blog you may find a better way to determine this - I use this tool:

    http://www.sharepointblogs.com/mossman/archive/2007/04/11/u2u-caml-query-builder.aspx

    Please note this is not a recommendation - merely a tool I use to help build the CAML queries since I am not a SharePoint expert! :)

    Scott

  • UKaren, u can use SharePoint Explorer (latest version which support MOSS 2007) to see the actual values of field names.

  • Thank you Scott,

    Sorry am late to answer you. I have already find the way to do what I asked.

    I apologies that I thought  what you write in your example  I must type it as is. I understand later the changes.

    Thanks again.

  • Hi Giannis,

    That is great to hear! I am glad it is working for you now.

    Scott

  • Hi,

    Following can be used to get the Unique UserID to be passed in "Assigned to" in the task list.

    Private Function sGetUserID(ByVal sUserID As String) As String

               Dim strUserID As String = ""

               Dim ugp As New WSUG.UserGroup()

               ugp.Credentials = New NetworkCredential("User", "Pwd", "Domain")

               Dim NDUser As XmlNode = ugp.GetUserInfo(sUserID)

               Dim xnrUserInfo As XmlNodeReader = New XmlNodeReader(NDUser)

               While xnrUserInfo.Read()

                   If xnrUserInfo.Name = "User" Then

                       strUserID = xnrUserInfo.GetAttribute("ID")

                   End If

               End While

               Return strUserID

           End Function

  • Scott,

    Thanks for getting back to me.  Through much more extensive testing of many different scenarios/forms I've come to believe something is wrong with the installation of my Forms Server.  Even when I create a simple form with only one text box I receive multiple errors:

    1) When publishing, InfoPath says that the form will not be able to be opened from within a browser - "Invalid Form Template."

    2) "Unknown Error" on the SharePoint site when attempting to Create New from within the form library.

    3) "Unknown Error" on the SharePoint Server when attempting to upload a form directly to the server (not by publishing).

    If you have any suggestions I'll take them, but I'm pretty sure this is NOT a problem with your example.

    Thanks,

    Matt Callaway

  • I tried publishing this completed form as a browser enabled content type.  The form will launch, but it will not submit.  I get an error when I hit submit and the list does not update.  I have even added some custom code I found online and have been unsuccessful in making this form work in a browser.  Has anyone been able to do it?  

    Regards,

    Brian

    -To be clear, the form works perfectly fine when launched from inside the Infopath client, but not from the browser enabled infopath form setup as a content type in a document library.

  • Hi Brian,

    I just tested this process after pubishing my XSN as a Site Content Type and it worked from the browser without issue.

    If you would, try these steps:

    - Delete the Content Type from the document library where you added the published content type

    - Delete it from the Site Content Type list

    - Delete the XSN from the saved location

    - Create a new library on the same site called: ContentTypes

    - Publish the XSN as a Site Content Type and save it in the ContentTypes library

    - Add it as a Content Type to your document library

    - Test

    Scott

  • Hi Scott,

    I am trying to allow anonymous users to submit items to Sharepoint List using "Infopath Form Services" over the browser, but I am facing some issues.

    In order to achieve this, I have created a web service as you have explained in here:

    http://blogs.msdn.com/infopath/archive/2007/03/26/submitting-to-a-sharepoint-list.aspx#2969087

    I can submit the form through infopath as "string as data", but when I try to submit it using form services over the browser, it fails returning an error: "Infopath cannot submit the form".

    Are anonymous users not allowed to add items to sharepoint list using infopath form services?

    Also, can this issue (allowing anonymous users to add items to sharepoint list through form services) be overcome with another method?

    Thanks in advance

Page 5 of 36 (530 items) «34567»