Ejemplo n.º 1
0
        public static string Stringify(string field, Entity record, IOrganizationService service, ConfigHandler config = null)
        {
            var value = record.GetAttributeValue <object>(field);

            if (value == null)
            {
                return(null);
            }

            var entityReference = value as EntityReference;
            var optionSet       = value as OptionSetValue;
            var money           = value as Money;

            if (entityReference != null)
            {
                return(record.FormattedValues.ContainsKey(field)
                        ? record.FormattedValues[field]
                        : entityReference.Name ?? entityReference.Id.ToString());
            }

            if (optionSet != null)
            {
                var textValue = optionSet.Value.ToString(CultureInfo.InvariantCulture);

                if (config == null)
                {
                    return(textValue);
                }

                var configLanguage = config.GetValue <int>("optionSetLcid", "optionSetLcid must be an int!");

                if (configLanguage == 0)
                {
                    return(textValue);
                }

                var request = new RetrieveAttributeRequest
                {
                    EntityLogicalName     = record.LogicalName,
                    RetrieveAsIfPublished = true,
                    LogicalName           = field
                };

                var response = service.Execute(request) as RetrieveAttributeResponse;
                var metadata = (PicklistAttributeMetadata)response.AttributeMetadata;

                var fieldMetadata = metadata.OptionSet.Options.First(f => f.Value == optionSet.Value);

                var label = fieldMetadata.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == configLanguage)?.Label;

                if (label != null)
                {
                    return(label);
                }

                return(fieldMetadata.Label.UserLocalizedLabel.Label);
            }

            if (money != null)
            {
                return(record.FormattedValues.ContainsKey(field)
                        ? record.FormattedValues[field]
                        : money.Value.ToString(CultureInfo.InvariantCulture));
            }

            return(value.ToString());
        }
Ejemplo n.º 2
0
        public static string Stringify(string field, Entity record, IOrganizationService service, ConfigHandler config = null)
        {
            var value = record.GetAttributeValue <object>(field);

            if (value == null)
            {
                return(null);
            }

            return(StringifyProperty(field, value, record, service, config));
        }