internal static dynamic TryConvertAttributeValue(OrganizationServiceContext context, string entityName, string attributeName, object value, Dictionary <string, AttributeTypeCode?> AttributeTypeCodeDictionary)
        {
            if (context == null || string.IsNullOrWhiteSpace(entityName) || string.IsNullOrWhiteSpace(attributeName))
            {
                return(null);
            }

            if (AttributeTypeCodeDictionary == null || !AttributeTypeCodeDictionary.Any())
            {
                AttributeTypeCodeDictionary = MetadataHelper.BuildAttributeTypeCodeDictionary(context, entityName);
            }

            object newValue          = null;
            var    attributeTypeCode = AttributeTypeCodeDictionary.FirstOrDefault(a => a.Key == attributeName).Value;

            if (attributeTypeCode == null)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format("Unable to recognize the attribute '{0}' specified.", attributeName));
                return(null);
            }

            try
            {
                switch (attributeTypeCode)
                {
                case AttributeTypeCode.BigInt:
                    newValue = value == null ? (object)null : Convert.ToInt64(value);
                    break;

                case AttributeTypeCode.Boolean:
                    newValue = value == null ? (object)null : Convert.ToBoolean(value);
                    break;

                case AttributeTypeCode.Customer:
                    if (value is EntityReference)
                    {
                        newValue = value as EntityReference;
                    }
                    else if (value is Guid)
                    {
                        var metadata  = MetadataHelper.GetEntityMetadata(context, entityName);
                        var attribute = metadata.Attributes.FirstOrDefault(a => a.LogicalName == attributeName);
                        if (attribute != null)
                        {
                            var lookupAttribute = attribute as LookupAttributeMetadata;
                            if (lookupAttribute != null && lookupAttribute.Targets.Length == 1)
                            {
                                var lookupEntityType = lookupAttribute.Targets[0];
                                newValue = new EntityReference(lookupEntityType, (Guid)value);
                            }
                        }
                    }
                    break;

                case AttributeTypeCode.DateTime:
                    newValue = value == null ? (object)null : Convert.ToDateTime(value).ToUniversalTime();
                    break;

                case AttributeTypeCode.Decimal:
                    newValue = value == null ? (object)null : Convert.ToDecimal(value);
                    break;

                case AttributeTypeCode.Double:
                    newValue = value == null ? (object)null : Convert.ToDouble(value);
                    break;

                case AttributeTypeCode.Integer:
                    newValue = value == null ? (object)null : Convert.ToInt32(value);
                    break;

                case AttributeTypeCode.Lookup:
                    if (value is EntityReference)
                    {
                        newValue = value as EntityReference;
                    }
                    else if (value is Guid)
                    {
                        var metadata  = MetadataHelper.GetEntityMetadata(context, entityName);
                        var attribute = metadata.Attributes.FirstOrDefault(a => a.LogicalName == attributeName);
                        if (attribute != null)
                        {
                            var lookupAttribute = attribute as LookupAttributeMetadata;
                            if (lookupAttribute != null && lookupAttribute.Targets.Length == 1)
                            {
                                var lookupEntityType = lookupAttribute.Targets[0];
                                newValue = new EntityReference(lookupEntityType, (Guid)value);
                            }
                        }
                    }
                    break;

                case AttributeTypeCode.Memo:
                    newValue = value as string;
                    break;

                case AttributeTypeCode.Money:
                    newValue = value == null ? (object)null : Convert.ToDecimal(value);
                    break;

                case AttributeTypeCode.Picklist:
                    var plMetadata  = MetadataHelper.GetEntityMetadata(context, entityName);
                    var plAttribute = plMetadata.Attributes.FirstOrDefault(a => a.LogicalName == attributeName);
                    if (plAttribute != null)
                    {
                        var picklistAttribute = plAttribute as PicklistAttributeMetadata;
                        if (picklistAttribute != null)
                        {
                            int            picklistInt;
                            OptionMetadata picklistValue;
                            if (int.TryParse(string.Empty + value, out picklistInt))
                            {
                                picklistValue = picklistAttribute.OptionSet.Options.FirstOrDefault(o => o.Value == picklistInt);
                            }
                            else
                            {
                                picklistValue = picklistAttribute.OptionSet.Options.FirstOrDefault(o => o.Label.GetLocalizedLabelString() == string.Empty + value);
                            }

                            if (picklistValue != null && picklistValue.Value.HasValue)
                            {
                                newValue = value == null ? null : new OptionSetValue(picklistValue.Value.Value);
                            }
                        }
                    }
                    break;

                case AttributeTypeCode.State:
                    ADXTrace.Instance.TraceWarning(TraceCategory.Application, string.Format("Attribute '{0}' type '{1}' is unsupported. The state attribute is created automatically when the entity is created. The options available for this attribute are read-only.", attributeName, attributeTypeCode));
                    break;

                case AttributeTypeCode.Status:
                    if (value == null)
                    {
                        return(false);
                    }
                    var optionSetValue = new OptionSetValue(Convert.ToInt32(value));
                    newValue = optionSetValue;
                    break;

                case AttributeTypeCode.String:
                    newValue = value as string;
                    break;

                default:
                    ADXTrace.Instance.TraceWarning(TraceCategory.Application, string.Format("Attribute '{0}' type '{1}' is unsupported.", attributeName, attributeTypeCode));
                    break;
                }
            }
            catch (Exception ex)
            {
                WebEventSource.Log.GenericWarningException(ex, string.Format("Attribute '{0}' specified is expecting a {1}. The value provided is not valid.", attributeName, attributeTypeCode));
            }
            return(newValue);
        }
示例#2
0
 public EntityExpressionEvaluator(OrganizationServiceContext context, Entity entity)
 {
     ServiceContext = context;
     EvaluateEntity = entity;
     AttributeTypeCodeDictionary = MetadataHelper.BuildAttributeTypeCodeDictionary(context, entity.LogicalName);
 }