WSS3 - setting a default value on a SPFieldType.Boolean after creation

6.8k Views Asked by At

I'm using WSS3 and C# to create site and I am creating a class to change fields on lists after they have been created. I have already created an SPField.Boolean type with no default value, but after upgrade I need the default value to be set to true. My current code that does not work follows:

           //web is already defined as the current web
           var list = web.Site.RootWeb.Lists["ListWithFieldOnIt"];
           var field = list.Fields.GetField("booleanfield");
           field.DefaultValue = "1";
           field.Update(true);
           list.Update(true);

I have tried to change the default value through the sharepoint instance and sharepoint manager 2007 and neither of those have worked. Does anyone know of any way to set the default value or what I am doing wrong?

Thanks in advance

2

There are 2 best solutions below

0
On BEST ANSWER

Code below should be more than enough to update list field definition:

       var list = web.Site.RootWeb.Lists["ListWithFieldOnIt"];
       var field = list.Fields.GetField("booleanfield");
       field.DefaultValue = "1";
       field.Update();

You don't need to update the list or pass 'true' to SPField.Update method.

0
On

Looks like you're doing it correctly according to Programmatically setting the default value of a SPFieldBoolean field. I can't see anything really wrong. My only suggestion would be to try the Update calls without the boolean parameter. From MSDN, SPField.Update Method (Boolean) seems to be meant for site columns rather than columns within a list. Whenever I'm updating a field or list in code, I almost always use the parameterless Update method.