Programmatically add an item with a Rich Text field to a SharePoint list using InfoPath 2007

Applies to: InfoPath 2007

Programmatically add an item to a SharePoint list by submitting a CAML update batch to the UpdateListItems method of the Lists web service that comes with Windows SharePoint Services (WSS).

ADVERTISEMENTS

Problem

You have a SharePoint list that contains a Rich Text column, which you want to fill with the contents of a Rich Text field from an InfoPath form.

Solution

Programmatically add an item to a SharePoint list by submitting a CAML update batch to the UpdateListItems method of the Lists web service that comes with Windows SharePoint Services (WSS).

Discussion

You can accomplish this functionality as follows:

  1. Create a custom SharePoint list that contains an extra column named Notes of type Multiple lines of text and type of text to allow Enhanced rich text (Rich text with pictures, tables, hyperlinks).
  2. Design an InfoPath form template as shown in figure 1 (Notes is a Rich Text Box control)

    Figure 1. The InfoPath form template in design mode.

    with a Main data source that resembles the following figure:


    Figure 2. The Main data source of the form template.

    Here the listName field node has its Default Value set equal to the GUID (globally unique identifier) of the SharePoint list you want to add the item to. To find out what this GUID is, in SharePoint, navigate to the custom list that you created in step 1, choose Settings > List Settings, and from your browser's Address bar, copy the GUID that comes after List=.

    Note: GUIDs in SharePoint 2007 look something like %7BD5E6FE0D%2D362E%2D4E63%2DB12C%2D823C3BECF477%7D. You will have to replace %7B with {, %2D with -, and %7D with }. You can leave out the curly brackets when using the GUID in InfoPath, so that your final modified GUID looks something like D5E6FE0D-362E-4E63-B12C-823C3BECF477.
  3. Create an XML file with the following contents and call it CustomListCAML.xml: <?xml version="1.0" encoding="UTF-8" ?>
    <Batch>
    <Method ID="1" Cmd="New">
    <Field Name="Title" />
    <Field Name="Notes" />
    </Method>
    </Batch>
  4. In InfoPath, click on Tools > Data Connections and add a Receive data data connection to the XML document you created in the previous step. Name the data connection CustomListCAML.
  5. While you are still in the Data Connections dialog box, add a Submit data data connection To a web service, and click Next.

    Enter the URL to the Lists web service on the top-level site or subsite where the custom list is located, and click Next. For example:
    http://<Server>/sites/<Site>/_vti_bin/Lists.asmx
    uses the Lists web service of a subsite called <Site>.

    Note: Each top-level site and subsite in SharePoint has its own Lists web service, so you must ensure that you've got the right one, otherwise you'll get back an error telling you that the list cannot be found for the site you specified.

    Choose UpdateListItems as the web method to use, and click Next.

    Specify the listName field node of the Main data source as the value for the tns:listName parameter (accept the default Include setting of Text and child elements only), and specify the Batch group node of the CustomListCAML secondary data source as the value for the tns:updates parameter. Select XML subtree, including selected element from the Include drop-down list box for submitting the Batch group node. Click Next.

    Change the name of the data connection to CustomListItemSubmit, and click Finish. Close the Data Connections dialog box.
  6. Double-click the Submit button to open its Properties dialog box, and click Edit Form Code to add a Clicked event handler for the Submit button. Add the following C# code to the Clicked event handler InfoPath created for you: XPathNavigator root = MainDataSource.CreateNavigator();

    // Retrieve the values for the custom list item
    string title = root.SelectSingleNode("my:myFields/my:title",
    NamespaceManager).Value;
    string notes = root.SelectSingleNode("my:myFields/my:notes",
    NamespaceManager).InnerXml;

    XPathNavigator batch = DataSources["CustomListCAML"].CreateNavigator();

    // Set the title
    batch.SelectSingleNode("/Batch/Method/Field[@Name='Title']",
    NamespaceManager).SetValue(title);

    // Set the notes
    batch.SelectSingleNode("/Batch/Method/Field[@Name='Notes']",
    NamespaceManager).SetValue(notes);

    // Submit the item details to the web service to update the custom list
    DataConnections["CustomListItemSubmit"].Execute();

    Or add the following code if you want to use Visual Basic code instead of C# code:
    Dim root As XPathNavigator = MainDataSource.CreateNavigator()

    ' Retrieve the values for the custom list item
    Dim title As String = root.SelectSingleNode("my:myFields/my:title", NamespaceManager).Value
    Dim notes As String = root.SelectSingleNode("my:myFields/my:notes", NamespaceManager).InnerXml

    Dim batch As XPathNavigator = DataSources("CustomListCAML").CreateNavigator()

    ' Set the title
    batch.SelectSingleNode("/Batch/Method/Field[@Name='Title']", _
    NamespaceManager).SetValue(title)

    ' Set the notes
    batch.SelectSingleNode("/Batch/Method/Field[@Name='Notes']", _
    NamespaceManager).SetValue(notes)

    ' Submit the item details to the web service to update the custom list
    DataConnections("CustomListItemSubmit").Execute()

You should now have a fully functional form so that when you click on the Submit button after filling out the InfoPath form, an item will be added to the custom list in SharePoint.

 


Related InfoPath Articles:

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services