The SharePoint internals behind an Append text field

189

SharePoint internals of the Append text field

In configuring a SharePoint field, there’s an option to “Append Changes to Existing Text” for Multi-line text fields. This setting requires versioning be enabled. Let’s delve into why versioning must be enabled for this feature to be enabled.

In a nutshell, SharePoint puts the latest value within the property bag, which you can see if you grab the SPItem object, and dump the read-only SPItem.xml. First let’s examine in PowerShell.

# let's get the web:
$web=Get-SPWeb http ://SharePoint/sites/Site/web

# let's get the list:
$list = $web.Lists["Name of List"]

# let's get the SPItem:
$q=$list.GetItemById(4567)

#Let's set the internal field name for later reference:
$FName = "Internal Field Name"

A more elegant way is by addressing by URL directly:

$url="http ://SharePoint/sites/Site/web/Listname/4567_.000"
$item = $web.GetListItem($url)

Then we can peek into the property bag:

$item.xml

It is worth noting there are times when SharePoint does not put the latest value in the property bag. In fact the property is absent altogether, although the versions continue to capture the append text. One theory is that this occurs to SP2007 lists that have been upgraded. If you experience this behavior, read below for accessing all of the append text.

But where are the previous appends? How can we see them? The trick is to walk through the versions, grabbing the version SPItem, and then grab the field value.

foreach ($v in $item.versions)
{
 $v.get_Item($FName)
}

In C# we extract in a function such as this:

public static string GetVersionedMultiLineTextAsPlainText(int ID, string field,SPList list)
 {
     SPListItem item = list.GetItemById(ID);
     StringBuilder sb = new StringBuilder();
     foreach (SPListItemVersion version in item.Web.Lists[item.ParentList.ID].Items[item.UniqueId].Versions)
     {
         SPFieldMultiLineText CommentsField = version.Fields.GetFieldByInternalName(field) as SPFieldMultiLineText;
         if (CommentsField != null)
         {
             string comment = CommentsField.GetFieldValueAsText(version[field]);
             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();
 }

Share this entry

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents

Categories

Categories