Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();
    }
}
share|improve this question
    
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
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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