SharePoint Dragons

Nikander & Margriet on SharePoint

SPMetal looses spaces in choice field

Recently, we were creating a web part that displays entities generated with SPMetal. One of the columns is a status field called MyStatus of type Choice. In the display, it just shows “InReview”, when the actual value is “In Review”.

The code does something similar to this:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Linq;

namespace VisualWebPartProject1.VisualWebPart1
    public partial class VisualWebPart1UserControl : UserControl
        AstroDataContext dc = new AstroDataContext(SPContext.Current.Web.Url);
        protected override void OnPreRender(EventArgs e)
            var query = (from item in dc.MyPrintList
                                     select item).ToList<MyPrintBaseType>();

            gvOverview.AutoGenerateColumns = false;
            gvOverview.DataSource = query;

The declarative part looks something like this:

<asp:GridView runat=”server” ID=”gvOverview” AllowPaging=”False”>
        <asp:TemplateField HeaderText=”Print”>
                <input type=”checkbox” id=’chkAvail<%#DataBinder.Eval(Container.DataItem, “Id”)%>’
                    value='<%#DataBinder.Eval(Container.DataItem, “Id”)%>’ checked />
        <asp:BoundField HeaderText=”ID” DataField=”ID” />
        <asp:BoundField HeaderText=”Title” DataField=”Title” />           
        <asp:TemplateField HeaderText=”Status”>
                <div id=”divStatus<%#DataBinder.Eval(Container.DataItem, “Id”)%>”>
                <%#DataBinder.Eval(Container.DataItem, “MyStatus”)%>                   

        <asp:TemplateField HeaderText=”Date”>
                <asp:Label ID=”Label1″ Text='<%#DataBinder.Eval(Container.DataItem, “MyDate”,”{0:dd-MM-yyyy}”)%>’
                    runat=”server” />

This result be seen in the next figure.


So, somebody stole our spaces! Our immediate response to this brutal case of space theft was to find the bandit responsible for this. We had one main suspect…

SPMetal! When we took a look at the code generated by SPMetal, it’s quite clear why the spaces are missing. The original, full-blown, values are stored as attributes in the MyStatus enumeration. However, the actual names within the enumeration, as generated by SPMetal, are used when displayed in the SPGridView:

public enum MyStatus : int {

None = 0,

Invalid = 1,

Unread = 2,

[Microsoft.SharePoint.Linq.ChoiceAttribute(Value=”In Review”)]
InReview = 4,

Accepted = 8,

Denied = 16,

Remark = 32,


As is explained in , this is normal behavior. Since SPMetal generates partial classes, one way to get around it is to create a partial class for the entity representing your content type that provides a method or property that returns the attribute associated with a given enumeration value. Another way would be to generate an extension method that does the same. The implementation of such an extension method is shown in the answer in thread .

Another way, and we guess the cleanest way if you’re only reading data, is to override the SPMetal defaults by using a Parameters XML file. See for details.

In our case, we’ve saved the following XML in a file called params.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<Web xmlns=””>
  <ContentType Name=”MyPrintBaseType” Class=”MyPrintBaseType”>
    <Column Name=”MyStatus” Member=”MyStatus” Type=”String” />

This XML file says that we want to convert our MyStatus field to the type of String, which causes the original values to be included. See , it also discusses a scenario where you want to write to a choice field too.

Then, we’ve generated our entities anew using the parameters XML file:

spmetal /web:http://astro /code:Astro.cs /parameters:params.xml

The end result meets our needs:


One response to “SPMetal looses spaces in choice field

  1. Pingback: Link Resource # 54 : May 11 – May 17 « Dactylonomy of Web Resource

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: