Monday, March 10, 2008

Display choices using checkboxes (allow multiple selections)

In this post I'll show you how to make a multichoice site column. First I was looking in the SDK for an appropriate value for the attribute "Format" of a choice field.

The MOSS SDK says the following about the Format attribute:

For Choice fields, this attribute can be set to Dropdown (default), which provides editing options through a drop-down selection, or to RadioButtons, which provides options through a group of radio buttons.

If you have already created a choice site column with the UI in SharePoint you will have noticed that you have several display modes of a choice field:

Display choices using:
  • Drop-Down Menu
  • Radio Buttons
  • Checkboxes (allow multiple selections)

So I thought the display modes were related to the Format Attribute of the Choice field (Isn't that logic?). I was wrong! After a while I found another (the correct) type to realize a multichoice checkbox thing. The type is "MultiChoice":

Specifies a Choice field that implements check boxes and allows the user to select multiple values.

An example of how to make a multichoice field with a feature...

<.Field ID="{EB21EEF7-9B67-4eb8-8CC9-15F76264121F}" Name="Vacature Product" Group="Vacature Page Layout Site Columns" DisplayName="Product" SourceID="" StaticName="Vacature Product" Type="MultiChoice" Required="TRUE" FillInChoice="TRUE">
<.CHOICE>Marketing & Product Management<./CHOICE.>
<.CHOICE>Sales & Services<./CHOICE.>
<.CHOICE>ICT & Design<./CHOICE.>
<.CHOICE>Human Resources<./CHOICE.>
<.CHOICE>Financieel & Business analyse<./CHOICE.>

After the site column creation, you can use this fieldtype in a pagelayout with the CheckBoxChoiceField class.

<.SharePointWebControls:CheckBoxChoiceField FieldName="Product" runat="server" ID="CheckBoxChoiceField1"/.>


Anonymous said...

Thanks for this information...! I was testing each control to find the correct one...

saksham said...

Absolutely right. i was fighting with this for couple of hours. giving "checkbox" as format was logical but then its sharepoint.

saksham said...

absolutely. keeping "checkbox" as format was logical. i was fighting with this for couple of hours. thanks

Milan said...

Gr8, but how to select the default value?
Because, when I set the default value, than this value is not checked on page.

So there are only unchecked checkboxes, but no one default value (checked checkbox)
Thank you