SharePoint Calculated Columns – Server and Client-Side Difference

I have just completed a task to improve performance of a Silverlight web part that queried a list and returned a large amount of fairly static data. To improve performance the client-side code that performed the query using the Silverlight client object model was extracted, converted to the full server API and placed in a custom WCF service hosted within SharePoint. The client code then called the service, which queried the list, cached the results and then returned them, to the client. Future calls were thus served from the cache dramatically reducing the time taken for the client to obtain the data. In doing this I came across an interesting difference when it came to retrieving and manipulating the values of calculated columns.

Imagine a SharePoint list, which has had a custom column of field type calculated added, named Full Name. The return value type of the column is single line of text and the formula concatenates two columns (say First Name and Last Name) and returns a persons full name.

Now to get the value out using server side code is very familiar:

using (SPSite site = new SPSite("http://intranet.3guysonsharepoint.com"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists.TryGetList("Clients");
        SPListItemCollection items = list.Items;

        foreach (SPListItem item in items)
        {
            SPField field = item.Fields["FullName"];
            Console.WriteLine("Title={0}", item.Title);
            Console.WriteLine("Full name={0}", item["FullName"].ToString());
            Console.WriteLine("SPListItem.GetFormattedValue={0}", item.GetFormattedValue("FullName"));
            Console.WriteLine("SPField.GetFieldValue={0}", field.GetFieldValue(item["FullName"].ToString()));
            Console.WriteLine("SPField.GetFieldValueAsHtml={0}", field.GetFieldValueAsHtml(item["FullName"]));
            Console.WriteLine("SPField.GetFieldValueAsText={0}", field.GetFieldValueAsText(item["FullName"]));
            Console.WriteLine("SPField.GetFieldValueForEdit={0}", field.GetFieldValueForEdit(item["FullName"]));
            Console.WriteLine();
        }
    }
}

The value returned by item["FullName"].ToString() is actually string;#John Smith. The additional lines show different ways of obtaining the item field value – the output of the above is


Title=Test item
FullName=string;#John Smith
SPListItem.GetFormattedValue=John Smith
SPField.GetFieldValue=string;#John Smith
SPField.GetFieldValueAsHtml=John Smith
SPField.GetFieldValueAsText=John Smith
SPField.GetFieldValueForEdit=John Smith

OK. So we need to be aware of this when extracting the value of a calculated column programmatically on the server.

So what is the big difference using the client API?? Consider the equivalent client-side code…

ClientContext context = new ClientContext("http://intranet.3guysonsharepoint.com");
List list = context.Web.Lists.GetByTitle("Clients");
ListItemCollection listItems = list.Items();
context.Load(listItems, items => items.Include(
    item => item["Title"],
    item => item["FullName"]
    ));
ctx.ExecuteQueryAsync((s, args) =>
{
    Dispatcher.BeginInvoke(() =>
    {
        ListItem item = listItems[0];
        label1.Content = string.Format("Title: {0}, FullName: {1}", item.Title, item["FullName"]);
    });
}, (s, args) =>
{
    Dispatcher.BeginInvoke(() =>
    {
        label1.Content = args.Message;
    });
});

As you can see we are just outputting the raw field value by using the client-side equivalent of direct field value access by specifying the field name as the indexer. So what would you expect? string;#John Smith? Wrong obviously! It would not be different if it did!

We actually see the formatted value being output – John Smith.

Interesting eh?

This entry was posted in SharePoint 2010 and tagged . Bookmark the permalink.

Comments are closed.