Search code examples
c#sharepointspfield

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


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


Solution

  • 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.