After pretty exciting results with retrieving list data from SharePoint using ColdFusion 9, I’ve been quite eager to try out some of the other methods exposed by ColdFusion.  However, retrieving lists is one thing; testing out methods that could actually modify data is another entirely.  So, I broke down and spent several hours installing WSS and SQL Server 2008 on my local machine.  Interestingly enough, there are ways to get it to run on a 32-bit Vista machine, so if you’re curious how, let me know ;).

Anyway, once I got SharePoint installed, I immediately created a simple list.  It’s incredibly simple–it only has two fields, “Title” and “Favorite Color.”

The first method I wanted to try out was to add a list item to this list, a name and a color.  As with the list retrieval web service, the “updateListItems” method is just as simple to implement.  Here’s the code I used:

<cfsavecontent variable="xml">
<Batch OnError='Return'>
<Method ID='1' Cmd='New'>
<Field Name='ID'>New</Field>
<Field Name='Title'>Monkey</Field>
<Field Name='Favorite_x0020_Color'>Pink</Field>
</Method>
</Batch>
</cfsavecontent>

<cfset newItem = xmlParse(xml)>
<cfsharepoint action="updatelistitems"
              domain="localhost"
              userName="username"
              password="password"
              name="newitems"
              params="#{listName="AAC20C17-A87D-4985-BA1D-9DD511EE9B9A", updates="#newItem#"}#"
/>
<cfdump var="#newitems#" />

This is super straight-forward.  First, you create a “Batch” element.  Basically, this allows you to define any number of “methods” that you want to run in the overall “updatelistitems” call; in other words, you could loop over a bunch of new data, create a new method node for each, and insert several records in one call.  I won’t explain it in too much detail here…the MSDN docs do a better job, anyway :)

Once the Batch is created, you simply call the “updatelistitems” method, passing your credentials, the list name (GUID), and the “updates” XML which contains the Batch.  You can even specify a return object (“name”), and use it to populate data for other SharePoint calls.

Simple, huh?

The one thing to watch out for here is in the “Field” blocks in the Batch XML.  When you’re browsing a list’s properties in SharePoint, it’s important to remember that the field names you see are ONLY the display names for those fields.  In actuality, there is a REAL name that SharePoint requires for updating fields with data.  For example, in my list, the display name for the field “Favorite Color” is exactly that–“Favorite Color.”  However, SharePoint’s REAL name for it is “Favorite_x0020_Color”, and it will only accept this value in the “Field” section.

Yep, so that’s the “updatelistitems” method.  Of course, you can extend this beyond just creating new list items.  You could actually specify several create, update and delete methods in the same Batch if you wanted–I didn’t, just because I wanted to keep this example simple.  So yeah, I’m starting to get some really neat ideas about what can be done by merging CF and SharePoint…hopefully, I’ll have a context in which to actually do it :)