Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

As stated on MSDN, the CHOICE element contains the value within its body.

That means that in a field, if I have:

<CHOICES>
    <CHOICE>foo</CHOICE>
    <CHOICE>bar</CHOICE>
</CHOICES>

the HTML output is:

<option value="foo">foo</option>
<option value="bar">bar</option>

This is a problem for me as I need the value to be different than the label. What I need is this:

<option value="CODE1">foo</option>
<option value="CODE2">bar</option>

I also tried this:

<CHOICES>
    <CHOICE Value="CODE1">foo</CHOICE>
    <CHOICE Value="CODE2">bar</CHOICE>
</CHOICES>

But the result is still this:

<option value="foo">foo</option>
<option value="bar">bar</option>

Is there a way to specify different value than the label in a field of type Choice?

share|improve this question
    
Just as a reference. Your question seems to be somewhat related to mine here. Fell free to have a look, but be aware that I haven't still been able to make that caml property do anything usefull.. –  SPArchaeologist Jan 16 '12 at 8:36
add comment

3 Answers 3

up vote 3 down vote accepted

SPFieldChoice has a property named FieldRenderingControl. Using reflector you can find this code in the FieldRedneringControl property:

....
switch (this.EditFormat)
{
    case SPChoiceFormatType.Dropdown:
        control = new DropDownChoiceField();
        break;

    case SPChoiceFormatType.RadioButtons:
        control = new RadioButtonChoiceField();
        break;

    default:
        return null;
}
....

It seems that DropDownChoiceField is our control. Navigate to this control and in CreateChildControls you can see:

....
this.m_dropList.DataSource = this.DataSource;
this.m_dropList.DataValueField = "ValueField";
this.m_dropList.DataTextField = "TextField";
this.m_dropList.DataBind();
....

..lets go to DataSource property:

....
    DataTable table = new DataTable();
    table.Locale = CultureInfo.InvariantCulture;
    table.Columns.Add(new DataColumn("ValueField", typeof(string)));
    table.Columns.Add(new DataColumn("TextField", typeof(string)));
    if (string.IsNullOrEmpty((string) this.ItemFieldValue))
    {
        DataRow row = table.NewRow();
        row[0] = string.Empty;
        row[1] = string.Empty;
        table.Rows.Add(row);
        this.m_bHasBlankChoice = true;
    }
    for (int i = 0; i < base.NumberOfChoices; i++)
    {
        DataRow row2 = table.NewRow();
        row2[0] = ((SPFieldChoice) base.Field).Choices[i];
        row2[1] = row2[0];
        table.Rows.Add(row2);
    }
    this.m_dataSource = new DataView(table);
}
return this.m_dataSource;

row2[1] = row2[0] means that value attribute always equals to option's label and your experiments prove it (my answer is related to 2010 sharepoint)

If this behavior extremely needed, may be custom field type will be an option or any workarounds.

share|improve this answer
    
Thanks for the inside view :). I changed my application to use the label instead of values. It sucks but that's life... –  JohnDoDo Jan 23 '12 at 15:01
add comment

There is an explanation how to fix your problem, but I'm not sure that this solution is good, because you should change sharepoint fiels. I think that the best way is to change logic of your application.

share|improve this answer
add comment

Short answer: No.

The only (out of the box) ways to do this are:

  • Lookup field

    • (PRO: behaves as a choice field when rendered)
    • (CON: only works within 1 site only)
  • Managed metadata, adding the value as a synonym

    • (PRO: works everywhere)
    • (CON: is type ahead, so looks different from choice field)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.