Setting Boolean Values in a SharePoint SPItemEventReceiver

One of the confusing things about implementing an SPItemEventReceiver that modifies properties on an SPListItem is that while you are given an actual SPListItem with which to work, you’re really not supposed to use it. Instead, you should be modifying properties in the AfterProperties property of the SPItemEventProperties parameter passed into the method.

AfterProperties is an SPItemEventDataCollection object, which is really just a glorified hash table containing key/value pairs representing properties on the item and the value of those properties. There are two annoyances that I have found with using the AfterProperties to set values:

1.) You cannot use the GUID of a field to set a value, you have to use the textual name of the field. This means you need to use either the Name / Static Name of the field to set the values. In our projects, the Name / Static Name is set to the same value, so I’m really not 100% sure if Name works, Static name works, or they both work, so you may have to play around with it.

2.) You apparently can’t just set a Boolean value using true / false. You have to specify the value as “TRUE” or “FALSE” for the value to take, e.g.,

//DOES NOT WORK:
properties.AfterProperties[“FieldName”] = true;
//WORKS:
properties.AfterProperties[“FieldName”] = “TRUE”;

Based on how the fields are defined in content types, you may also have some success with “YES” and “NO” as values as well, but I know that the “TRUE” and “FALSE” values work fine. One of the notes in the documentation on Boolean values states:

When a Boolean column value in a list is set to true and the ItemUpdating method is called, the AfterProperties property reports -1 as the value instead.

This may have something to do with the oddities as well.