Setting Boolean Values in a SharePoint SPItemEventReceiver

Comments 0

Share to social media

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.

Load comments

About the author

Damon Armstrong

See Profile

Damon Armstrong is a consultant with SystemwarePS in Dallas, Texas. He is also a blogger and author of Pro ASP.NET 2.0 Website Programming and SharePoint 2013 Essentials for Developers. He specializes in the Microsoft stack with a focus on web technologies like MVC, ASP.NET, JavaScript, and SharePoint. When not staying up all night coding, he can be found watching a bunch of kids, studying Biblical topics, playing golf, or recovering from staying up all night coding.