ChoiceField operations

Filter a collection by ChoiceField value

Request type: GET

Format: {{baseurl}}/api/default/{{baseurl}}/api/default/{{entity}}?$filter=cast({{related_data_field_name}}, 'Edm.String') eq {{filter_value}} 

The CLR type to which a choice field is being resolved in the OData implementation is Enum. To filter by the value of a choice field, you must first use a cast() operation in the query string in order to cast the field's type to Edm.String.

NOTE: When working with ChoiceField through the OData web services, the field value is returned as a number (e.g. 2, 4, 8) instead of the actual selected textual value in this field (e.g. Option1, Option2, Option3). The returned value indicates which choice option has been selected. It is returned as the 2 to the power of the position of that choice in the collection of all choices (2n). For example, if you have configured your ChoiceField with 4 options - Option 1, Option 2, Option 3, Option 4 the value which will be returned by the service will be: 

If the selected option is Option 1, the service returns 1 (2 to the power of 0)
If the selected option is Option 2, the service returns 2 (2 to the power of 1)
If the selected option is Option 3, the service returns 4 (2 to the power of 2)
If the selected option is Option 4, the service returns 8 (2 to the power of 3)

Update an item and set its ChoiceField values

Request type: PATCH

Format: {{baseurl}}/api/default/{{entity}}({{item_id}})

To update the ChoiceField value of an item, send a PATCH request to the item’s entity URL and specify the item Id. In the request body, specify the desired ChoiceField value in JSON format. For example, if your choice field has 3 options, and you want to set the selected choice to be the second one use: { MyChoiceField: "4" }. The value you pass must be numeric and is calculated as 2 to the power of the position of the desired choice in the collection of all choices (2n). For more information refer to the Filter a collection by ChoiceField valueexample from this article.

Want to learn more?

Sign up for our free beginner training. Boost your credentials through advanced courses and certification.
Register for Sitefinity training and certification.

Was this article helpful?