The SharePoint Engie that could…

Stories from a SharePoint Engineer that isn't afraid of Visual Studio.

Programmatically setting the default value of a SPFieldBoolean field

with 3 comments

Here’s something I couldn’t seem to find documented anywhere…

If you’ve programmatically created a new Yes/No field and added it to SharePoint, you don’t want to set the default value like this:

booleanField.DefaultValue = "No";

or

booleanField.DefaultValue = "Yes";

Doing so caused the following error when I was trying to create folders programmatically in the List that was inheriting the Content Type with that field.

Microsoft.SharePoint.SPException: Invalid Yes/No value
A Yes/No field contains invalid data. It should be either 1 or 0. Please check the value and try again.

That made me scratch my head for a while since the property takes a string and the values in the UI when you set it is Yes or No. How you should set that property is:

booleanField.DefaultValue = "0";

or

booleanField.DefaultValue = "1";

Written by Henry

December 5, 2008 at 1:25 pm

3 Responses

Subscribe to comments with RSS.

  1. Again one of those big annoyances in SharePoint. Just like sometimes guids must be provided including brackets, the other time without brackets, of sometimes in CAML that there is a difference between true and TRUE… Welcome to the world of SharePoint…

    Serge van den Oever

    December 6, 2008 at 1:13 pm

  2. Chris

    December 8, 2008 at 7:49 am


Leave a Reply