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.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?

Next article

Widget operations