views:

13

answers:

1

How do I get the value from a SPFieldBoolean object? Do I simply cast it to a boolean or do I need to do something further with it?

I am fetching it in an EventReceiver class during an ItemAdded event from properties.ListItem["fieldname"].

If there is a chance the field might not exist (and be null), how do I check for that?

+1  A: 

The value is already a bool, you just need to type-cast it. All fields provide values in their native value-type — see also SPField.FieldValueType property that gives you the actual type in case you need to inspect it on runtime.

To make sure the field is contained in the list, just use the SPFieldCollection.ContainsField method on your list's Fields collection.

Ondrej Tucny
It won't let me compile using "bool field = (bool) properties.ListItem["field"]. Do I need to first make a SPFieldBoolean and then type cast from that (or use FieldValueType) ?
Christian P.
use "bool field = Convert.ToBoolean(properties.ListItem["field"])"
Tom Vervoort