Choices field in OData services returning position instead o

Posted by Eduardo Añíbarro on 13-May-2019 06:45

Hi,

I have a Choices field in a Dynamic Module with 5 choices and the values 1,2,3,4 and 5. When I call the OData service, the returned value for the field is the binary position of the element converted to decimal, instead of the actual value or text.

This makes the value useless, so I guess that's a bug. I'm testing in Sitefinity 11.2, not sure if that's also the case for SF 12.

All Replies

Posted by jread on 14-May-2019 15:21

I have used a calculated property to alleviate the need for conversion of binary in JS.  Here is a CalculatedPropery class you can add to your web service following the instructions here:  [View:https://www.progress.com/documentation/sitefinity-cms/for-developers-custom-calculated-properties:550:50]

Here is the class (Replace 'Features' with your property name):

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Web.Services.Extensibility;

namespace SitefinityWebApp.Helpers
{
	public class CustomChoicesCalculatedField : CalculatedProperty
	{
		/// <summary>
		/// What type of data is being returned, must be a simple type or something that is already in the service or marked with [DataContract]
		/// </summary>
		public override Type ReturnType
		{
			get
			{
				return typeof(ChoiceOption[]);
			}
		}
		/// <summary>
		/// Returns Choice option for Choices fields on the Jobs Dynamic Module
		/// </summary>
		/// <param name="items">Data beeing returned by the oData service</param>
		/// <param name="manager">Current Manager from the service</param>
		/// <returns></returns>
		public override IDictionary<object, object> GetValues(IEnumerable items, IManager manager)
		{
			var ret = new Dictionary<object, object>();
			foreach (IDataItem item in items)
			{
				var dynamicContent = item as DynamicContent;
				var choices = dynamicContent.GetValue<ChoiceOption[]>("Features");
				if (choices != null)
				{
					ret.Add(item, choices);
				}
			}

			return ret;
		}
	}
}

link to GIST: https://gist.github.com/jonathanread/4813f6bf1a7f611f8ff737e18b8ccb82
Posted by Eduardo Añíbarro on 15-May-2019 05:35

Thanks jread, that's a very good solution and probably the best choice to avoid touching the javascript once they fix it :)

This thread is closed