Append Changes To Multiple Line Text Field


Deal is, we had a Multiple Line Text field with Append changes checked.

What it actually does is to keep the 'sum' of all the changes to this field in the column. ie. whenever some new text is added to this column, the new text is added to the already existing column. So at any time you would only see the full text.

Behind the scenes, the column maintains multiple versions, one version for each change.  what is rendered is the sum of all versions put in together. 


Later, we wanted to remove this setting and only keep the last value. But if you uncheck the  'Append Changes' setting, nothing is rendered for this column. That is a problem, since we had thousands of existing entries and it was not possible to add the last value manually to the column. Also, in case you directly try accessing the column values by  listItem["ColumnName"] construct, this too will return empty string. So you simply cannot loop through the items and store the column value


The solution actually lies in not accessing the column value directly but by versions. So here is the whole process flow

Accessing the last text entered. Where spItem is the list item you get while iterating over the list.Items and 'Recommendations' is the internal column name for Multiple Line of Text

string text = GetVersionedMultiLineTextAsPlainText(spItem, "Recommendations");

spItm["Recommendations"] = text;             

The GetVersionMultiLineTextAsPlainText is simply looping through all the versions ( in descending order of their addition) and returning the text

public static string GetVersionedMultiLineTextAsPlainText(SPListItem item, string key)

            StringBuilder sb = new StringBuilder();          
            string comment = string.Empty;

            foreach (SPListItemVersion version in item.Web.Lists[item.ParentList.ID].Items[item.UniqueId].Versions)

                SPFieldMultiLineText field = version.Fields[key] as SPFieldMultiLineText;

                if (field != null)
                    comment = field.GetFieldValueAsText(version[key]);

                    if (comment != null && comment.Trim() != string.Empty)
            return comment;