I am currently writing some code that when a user presses my custom ribbon button I add a choice column to the current document list. I am using the AddAsXml to add the column, I add the choice list and set the default value, but after adding the column (field), existing items do not have a value set for this field. Do I need to iterate the whole library after adding this field or is there a way to force the field add to update the value for existing items.
Here is the code I am using:
using (var site = new SPSite(siteId))
{
string columnName = "MyMeatChoice";
// Check if the list contains the meat choice column
var list = site.RootWeb.Lists[rootListId];
var field = list.Fields.TryGetFieldByStaticName(IdShieldStatusColumn);
if (field == null)
{
string newField = string.Concat("<Field Type='Choice' DisplayName='",
columnName, "' Name='", columnName, "' ",
"Format='Dropdown' Required='TRUE' Indexed='TRUE'><CHOICES>",
"<CHOICE>Fish</CHOICE><CHOICE>Beef</CHOICE>",
"<CHOICE>Chicken</CHOICE><CHOICE>Emu</CHOICE>",
"<CHOICE>Platypus</CHOICE><CHOICE>Penguin</CHOICE>",
"</CHOICES><Default>Penguin</Default>",
"<Description>The type of meat that you would like to eat.",
"</Description></Field>");
list.Fields.AddFieldAsXml(newField, true,
SPAddFieldOptions.AddFieldToDefaultView);
list.Update();
}
}