Posts Tagged ‘sharepoint’

In custom list we have field “Comments” and it’s type is Multi Line Text with Version and selected ”Allow append text” as option, now if we want to check all Versioned Comments /History of comments with entered text, date and time and person name then if we try to access it’s field value programmatically by list item then it will always returns latest version.
Below code will explain and demonstrate how we can take version of multiline text values with username, date and time.

public static string GetVersionedMultiLineTextAsPlainText(SPListItem item, string key)

{

StringBuilder sb = new StringBuilder();

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

{

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

if (field != null)

{

string comment = field.GetFieldValueAsText(version[key]);

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

{

sb.Append("");

sb.Append(version.CreatedBy.User.Name).Append(" (");

sb.Append(version.Created.ToString("MM/dd/yyyy hh:mm tt"));

sb.Append(") ");

sb.Append(comment);

}

}

}

return sb.ToString();

}