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 8 and 3 and type the answer here:
  • Post
  • Thanks alot for th reply Scott. yes I am using SharePoint 2010   , because of the limitations enforced by List customized using Infopath forms , I am looking at this approach. I am just able to design this form different rather than just inserting the secondary data repeating table as you have shown in the example. Is there any way we can redesign this form?

    Regards,

    Nandini

  • Hi Nandini,

    You cannot take a standard InfoPath form and make it into a form used in a SharePoint list. So what you may be able to do is:

    - For new entries: simply have a link on your SharePoint landing page that, when clicked on, will launch your form.

    - For editing existing rows of list data: this is a bit trickier: you may be able to add a hyperlink column to the list that, when clicked on, will query for the record ID of that list item and return just that data to your form. This will be a bit challenging to implement but may work. (NOTE: I have not actually tested this - merely a suggested possibility.)

    Scott

  • Hi,

    Are you able to use this form to update the information already in the list? Can you make changes in the display and when you hit the submit button it will submit the change to the list?

  • Hi Ashley,

    Yes - with a few modifications. I know there are a lot of comments but if you go through them you will find sample steps on how to use the process to update existing list items.

    Scott

  • Thanks Scott,

    I found your post from Oct 8, 2007 for writing the code. You said you added a new column with a button that would be used to update the row. Did you add this column after you already made the repeating table off of the MyContacts list? So will there be a button for each row? I need to be able to edit and delete the current items in the list.

    Right now I have a drop down that allows the user to select a value then the form displays the data from the sharepoint list for that value. Then I have the table where the user can add a new item and hit the button to submit the new item.

    Thanks!

  • I actually think I figured that part out. The question I have is where does the code go for the public void? Is this vba or #C?

  • Hi Ashley,

    It is C# and it would go on a button click event so when the button is clicked, it would execute the code to update the item you have specified.

    Scott

  • Great thanks Scott. This is the last question, sorry! I saw someone in an earlier post had the same problem but I get the error "the name 'e' does not exist in the current context". After researching it a bit it seems that the name needs to be declared before being called? Do you know how to fix this? The part of the code it pertains to is below:

    public void InternalStartup()

    {

    ((ButtonEvent)EventManager.ControlEvents["UpdateList"]).Clicked += new ClickedEventHandler(UpdateList_Clicked("Update", e.Source.CreateNavigator()));

    }

    public void UpdateList_Clicked(string strOperation, XPathNavigator e)

    {

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

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

  • Ashley - please post all your code so I can see what you have.

    Thanks...

  • public void InternalStartup()

    {

    ((ButtonEvent)EventManager.ControlEvents["UpdateList"]).Clicked += new ClickedEventHandler(UpdateList_Clicked("Update", e.Source.CreateNavigator()));

    }

    public void UpdateList_Clicked(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["Updated Headcount"];

     //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 xnTitle = e.SelectSingleNode("@Title", ns);

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

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

     XPathNavigator xnFranchise = e.SelectSingleNode("@Franchise", ns);

     XPathNavigator xnLocation = e.SelectSingleNode("@Location", ns);

     XPathNavigator xnPillar = e.SelectSingleNode("@Pillar", ns);

     XPathNavigator xnLeader = e.SelectSingleNode("@Leader", ns);

     XPathNavigator xnActivity = e.SelectSingleNode("@Activity", ns);

     XPathNavigator xnCategory = e.SelectSingleNode("@Category", ns);

     XPathNavigator xnPosition = e.SelectSingleNode("@Position", ns);

     XPathNavigator xnDeptNumber = e.SelectSingleNode("@DeptNumber", ns);

     XPathNavigator xnSupervisor = e.SelectSingleNode("@Supervisor", ns);

     XPathNavigator xnEmployeeType = e.SelectSingleNode("@EmployeeType", ns);

     XPathNavigator xnFTE = e.SelectSingleNode("@FTE", ns);

     XPathNavigator xnNotes = e.SelectSingleNode("@Notes", 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='Title'>" + xnTitle.Value + "</Field>" +

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

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

     "<Field Name='Franchise'>" + xnFranchise.Value + "</Field>" +

     "<Field Name='Location'>" + xnLocation.Value + "</Field>" +

     "<Field Name='Pillar'>" + xnPillar.Value + "</Field>" +

     "<Field Name='Leader'>" + xnLeader.Value + "</Field>" +

     "<Field Name='Activity>" + xnActivity.Value + "</Field>" +

     "<Field Name='Category'>" + xnCategory.Value + "</Field>" +

     "<Field Name='Position>" + xnPosition.Value + "</Field>" +

     "<Field Name='DeptNumber'>" + xnDeptNumber.Value + "</Field>" +

     "<Field Name='Supervisor'>" + xnSupervisor.Value + "</Field>" +

     "<Field Name='EmployeeType'>" + xnEmployeeType.Value + "</Field>" +

     "<Field Name='FTE'>" + xnFTE.Value + "</Field>" +

     "<Field Name='Notes'>" + xnNotes.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();      

    }

  • Thanks Ashley...so how are you getting that code into the "InternalStartup" procedure? Are you hand entering that? What happens if you:

    - Go back to InfoPath Designer

    - Right-click on the button and choose Button Properties

    - Click Edit Form Code

    What you have in InternalStartup does not seem correct.

    Scott

  • Yeah that's what I did originally but I think I got confused as to where to put the UpdateList("Update",e.Source.CreateNavigator) versus the rest of the code. Now when I go to right click on the button and hit edit form code, it won't open.

  • Ashley - delete what you have in InternalStartup then redo the steps from the button.

    Scott

  • Now it's saying that the "UpdateList" name does not exist in the current context. You said the only line in the Clicked event is the UpdateList("Update", e.Source.CreateNavigator()); correct? then do I just copy the procedure for UpdateList below that? Thanks so much for helping me out, I've been struggling with this for days!!

    public void InternalStartup()

    {

     ((ButtonEvent)EventManager.ControlEvents["UpdateList"]).Clicked += new ClickedEventHandler(UpdateList_Clicked);

    }

    public void UpdateList_Clicked(object sender, ClickedEventArgs e)

    {

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

    }

    public void UpdateList_Clicked(string strOperation, XPathNavigator e)

    {

Page 32 of 36 (530 items) «3031323334»