Programmatically add an item/event to a SharePoint 2003 calendar using InfoPath 2003 and script 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 2003 form.
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:
- 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 Events list for the calendar you want to add the item to, click Modify settings and columns, and from your browser's Address bar, copy the GUID that comes after List= (remove the curly braces from the GUID before using the GUID in InfoPath). -
Create an XML file with the following contents and call it EventCAML.xml:
Remark: SharePoint expects dates to be passed in the formats YYYY-MM-dd or YYYY-MM-ddTHH:mm:ssZ.
<?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" />
</Method>
</Batch>
- 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.
-
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:uses the Lists web service of a subsite called <Site>.http://<Server>/<Site>/_vti_bin/Lists.asmx
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 s0: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 s0:updates parameter. Select XML subtree, including selected element from the Include drop-down list box for submitting the Batch group node. Click Next.
Name the data connection Submit and click Finish. Close the Data Connections dialog box. -
Double-click on the Add button to open its Properties dialog box,
and click Edit Form Code to add a OnClick event handler for
the Add button. Add the following JScript code to the OnClick event
handler InfoPath created for you:
var root = XDocument.DOM;
// Retrieve the values for the calendar item
var title = root.selectSingleNode("my:myFields/my:title").text;
var location = root.selectSingleNode("my:myFields/my:location").text;
var startDate = root.selectSingleNode("my:myFields/my:startDate").text;
var startTime = root.selectSingleNode("my:myFields/my:startTime").text;
var endDate = root.selectSingleNode("my:myFields/my:endDate").text;
var endTime = root.selectSingleNode("my:myFields/my:endTime").text;
var batch = XDocument.DataObjects["EventCAML"].DOM;
// Set the location
batch.selectSingleNode("/Batch/Method/Field[@Name='Title']").text = title;
// Set the location
batch.selectSingleNode("/Batch/Method/Field[@Name='Location']").text = location;
// Set the start date
batch.selectSingleNode("/Batch/Method/Field[@Name='EventDate']").text = startDate + "T" + startTime + "Z";
// Set the end date
batch.selectSingleNode("/Batch/Method/Field[@Name='EndDate']").text = endDate + "T" + endTime + "Z";
// Submit the item details to the web service to update the calendar
XDocument.DataAdapters["Submit"].Submit();
Or add the following code if you want to use VBScript code instead of JScript code:
Dim root
Set root = XDocument.DOM
' Retrieve the values for the calendar item
Dim title, location, startDate, startTime, endDate, endTime
title = root.selectSingleNode("my:myFields/my:title").text
location = root.selectSingleNode("my:myFields/my:location").text
startDate = root.selectSingleNode("my:myFields/my:startDate").text
startTime = root.selectSingleNode("my:myFields/my:startTime").text
endDate = root.selectSingleNode("my:myFields/my:endDate").text
endTime = root.selectSingleNode("my:myFields/my:endTime").text
Dim batch
Set batch = XDocument.DataObjects("EventCAML").DOM
' Set the location
batch.selectSingleNode("/Batch/Method/Field[@Name='Title']").text = title
' Set the location
batch.selectSingleNode("/Batch/Method/Field[@Name='Location']").text = location
' Set the start date
batch.selectSingleNode("/Batch/Method/Field[@Name='EventDate']").text = startDate & "T" & startTime & "Z"
' Set the end date
batch.selectSingleNode("/Batch/Method/Field[@Name='EndDate']").text = endDate & "T" & endTime & "Z"
' Submit the item details to the web service to update the calendar
XDocument.DataAdapters("Submit").Submit
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.
Related InfoPath Articles:
