Programmatically add an item/event to a SharePoint 2007 calendar using InfoPath 2007 and .NET code

Use the UpdateListItems method of the Lists web service that comes with Windows SharePoint Services (WSS) to add an item to a SharePoint calendar from within an InfoPath 2007 form.

ADVERTISEMENTS

Problem

You want to use dates and other information that has been filled out in an InfoPath form to add an item/event to a calendar in SharePoint.

Solution

Since a SharePoint calendar is a SharePoint list, you can programmatically add an item/event to a SharePoint calendar 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. Design an InfoPath form template as shown in figure 1.

    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 calendar you want to add the item/event to. To find out what this GUID is, in SharePoint, navigate to the calendar you want to add the item to, 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 braces when using the GUID in InfoPath, so that your final modified GUID looks something like D5E6FE0D-362E-4E63-B12C-823C3BECF477.
  2. Create an XML file with the following contents and call it EventCAML.xml:
    <?xml version="1.0" encoding="UTF-8" ?>
    <Batch>
    <Method ID="1" Cmd="New">
    <Field Name="Title" />
    <Field Name="Location" />
    <Field Name="Description" />
    <Field Name="EventDate" />
    <Field Name="EndDate" />
    <Field Name="fAllDayEvent" />
    </Method>
    </Batch>
    Remarks:
    • SharePoint expects dates to be passed in the formats YYYY-MM-dd or YYYY-MM-ddTHH:mm:ssZ.
    • You can specify whether an event is an All Day Event by setting the value of the fAllDayEvent field to 1 and passing in dates that have the YYYY-MM-dd format.
  3. 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 EventCAML.
  4. 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 calendar 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 EventCAML 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.

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

    // Retrieve the values for the calendar item
    string title = root.SelectSingleNode("my:myFields/my:title",
    NamespaceManager).Value;
    string location = root.SelectSingleNode("my:myFields/my:location",
    NamespaceManager).Value;
    string startDate = root.SelectSingleNode("my:myFields/my:startDate",
    NamespaceManager).Value;
    string startTime = root.SelectSingleNode("my:myFields/my:startTime",
    NamespaceManager).Value;
    string endDate = root.SelectSingleNode("my:myFields/my:endDate",
    NamespaceManager).Value;
    string endTime = root.SelectSingleNode("my:myFields/my:endTime",
    NamespaceManager).Value;

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

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

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

    // Set the start date
    batch.SelectSingleNode("/Batch/Method/Field[@Name='EventDate']",
    NamespaceManager).SetValue(string.Format("{0}T{1}Z", startDate, startTime));

    // Set the end date
    batch.SelectSingleNode("/Batch/Method/Field[@Name='EndDate']",
    NamespaceManager).SetValue(string.Format("{0}T{1}Z", endDate, endTime));

    // Submit the item details to the web service to update the calendar
    DataConnections["Web Service Submit"].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 calendar item
    Dim title As String = root.SelectSingleNode("my:myFields/my:title", NamespaceManager).Value
    Dim location As String = root.SelectSingleNode("my:myFields/my:location", NamespaceManager).Value
    Dim startDate As String = root.SelectSingleNode("my:myFields/my:startDate", NamespaceManager).Value
    Dim startTime As String = root.SelectSingleNode("my:myFields/my:startTime", NamespaceManager).Value
    Dim endDate As String = root.SelectSingleNode("my:myFields/my:endDate", NamespaceManager).Value
    Dim endTime As String = root.SelectSingleNode("my:myFields/my:endTime", NamespaceManager).Value

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

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

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

    ' Set the start date
    batch.SelectSingleNode("/Batch/Method/Field[@Name='EventDate']", _
    NamespaceManager).SetValue(String.Format("{0}T{1}Z", startDate, startTime))

    ' Set the end date
    batch.SelectSingleNode("/Batch/Method/Field[@Name='EndDate']", _
    NamespaceManager).SetValue(String.Format("{0}T{1}Z", endDate, endTime))

    ' Submit the item details to the web service to update the calendar
    DataConnections("Web Service Submit").Execute()

You should now have a fully functional form so that when you click on the Add button after filling out the InfoPath form, an item/event will be added to the calendar of the SharePoint site.

 
 Subscribe for updates via RSS or Email

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 of S.Y.M. Wong-A-Ton. 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, S.Y.M. Wong-A-Ton.

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 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

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