Programmatically setting the default value of a SPFieldBoolean field

December 5th, 2008 § 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";
Advertisement

Tagged: , ,

§ 3 Responses to Programmatically setting the default value of a SPFieldBoolean field

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Programmatically setting the default value of a SPFieldBoolean field at The SharePoint Swiss Army Knife.

meta

Follow

Get every new post delivered to your Inbox.

Join 584 other followers