SharePoint Tip #27: Choice Columns and DataSheet Views

Using features to create Choice site columns is pretty straightforward (check this post for additional information). See below a sample feature element for that purpose.

<Field

    ID="{538c71e4-8650-4ce7-b021-920effa66346}"

    Type="Choice"

    Name="Publishing_x0020_Status"

    StaticName="Publishing_x0020_Status"

    DisplayName="Publishing Status"

    Required="FALSE

    Format="Dropdown"

    FillInChoice="FALSE"

    Group="Custom Columns">

    <Default>Not Published</Default>

    <CHOICES>

       <CHOICE>

          Not Published

       </CHOICE>

       <CHOICE>

          Pending

       </CHOICE>

       <CHOICE>

          Published

       </CHOICE>

       <CHOICE>

          Error

       </CHOICE>

    </CHOICES>

</Field>

Although the XML is well formed and all the attributes and values are correct, this field definition has a problem. If you create a site column using this definition, here is what happens:

  • If you check the site column configuration in SharePoint, everything will look fine
  • If you add this site column to a list (either through a content type, or directly) and you edit an item using the default list forms, everything will work as expected.
  • If you try to edit the list items using a DataSheet View, you won’t be able to select any value from this choice field and SharePoint will always throw a validation error.

The problem here are the invisible characters (spaces, newlines and tabs) between the <CHOICE> and </CHOICE> tags and their inner values. Apparently, when using list forms these characters are trimmed from the valid choices, but when using the datasheet view they are not, causing a strange behavior when editing an item in that view.

The correct definition for this field would be:

<Field

    ID="{538c71e4-8650-4ce7-b021-920effa66346}"

    Type="Choice"

    Name="Publishing_x0020_Status"

    StaticName="Publishing_x0020_Status"

    DisplayName="Publishing Status"

    Required="FALSE

    Format="Dropdown"

    FillInChoice="FALSE"

    Group="Custom Columns">

    <Default>Not Published</Default>

    <CHOICES>

       <CHOICE>Not Published</CHOICE>

       <CHOICE>Pending</CHOICE>

       <CHOICE>Published</CHOICE>

       <CHOICE>Error</CHOICE>

    </CHOICES>

</Field>

Leave a Reply

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