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>",
            "<Description>The type of meat that you would like to eat.",

            list.Fields.AddFieldAsXml(newField, true,
  • Have a look at msdn.microsoft.com/en-en/library/ms451470.aspx – Dennis G Jan 13 '11 at 9:20
  • @moontear that link is entirely unhelpful for this issue. I know how to add the field (see example posted), my question was whether there was a way to enforce the default value for existing items. There is not a way, you have to iterate the list of existing items and set those. I ended up using a simple CAML query to get all items that had a value of NULL for my new field. I then set those items. – pstrjds Jan 14 '11 at 18:15

You need to iterate each existing items. The data contained in an item is not affected by the new fields. It only adds a new property accessible to the item. In fact, it is better like this, just to prevent to override existing values.

  • Thanks for the response, and that makes sense that it would not alter existing items. I had read that even if you mark the field as "required" that it still allows "null" in the database, just the UI will prompt for the value. My suspicion was I would have to iterate the list. – pstrjds Jan 4 '11 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.