0
votes

I am new to sharepoint, I have a custom field type derived from SpFieldChoice , my field allows users to select multiple values, I have a requirement of replacing some old custom columns with the new column and copy the data in old column to the new column. the old column also allows the users to select multiple values by ticking checkboxes, I have the following code to copy the data to new field.

foreach (SPListItem item in list.Items)
{
    if (item[oldField.Title] == null)
    {
        item[newFld.Title] = string.Empty;
        item.Update();
    }
    else
    {
        string[] itemvalues = item[oldField.Title].ToString().Split(new string[] {";#"}, StringSplitOptions.None);
        StringBuilder multiLookupValues = new StringBuilder();
        multiLookupValues.Append(";#");
        for (int cnt = 0; cnt < (itemvalues.Length) / 2; cnt++)
        {
           multiLookupValues.Append (itemvalues[(cnt * 2) + 1].ToString() + ";#");
        }
        item[newFld.Title] = multiLookupValues.ToString();  
        item.SystemUpdate(false) ;
    }
}

This code works fine until the length of resulting stringbuilder is less than 255 charachters , but when this length is greater then 255 I get the following Exception. Invalid choice Value. A choice field contains invalid data.Please check the value and try again.

Is there any other way of copying data to SpFiledChoice, How can I resolve this problem? please help me.

1

1 Answers

0
votes

Do the update multiple times so that the string doesn't exceed - i.e. value +=. However, if the problem is that the value can't be longer that 255, you have to consider how you are doing the choices. If it is exceeding the length and updating the value multiple times doesn't work (and a Site Column will have the same limitations), you can do the next best thing:

1) Create a new list that will hold the choices 2) Change the destination field to be a lookup 3) Update accordingly for each item (picking up the ID from the lookup field)

There's no limit to this.

David Sterling

david_sterling@sterling-consulting.com

www.sterling-consulting.com