public static object Convert(object value, Dynamics365Entity entity, string attributeLogicalName, Dynamics365Connection connection, string targetEntityLogicalName)
        {
            object                   convertedValue    = default(object);
            EntityMetadata           entityMetadata    = entity.GetEntityMetadata(connection);
            AttributeMetadata        attributeMetadata = entityMetadata.Attributes.FirstOrDefault(findField => findField.LogicalName == attributeLogicalName);
            Dynamics365TypeConverter converter         = new Dynamics365TypeConverter(attributeMetadata, targetEntityLogicalName);
            Type destinationType = converter.Dynamics365Type;

            if (converter.CanConvertTo(destinationType))
            {
                try
                {
                    convertedValue = converter.ConvertTo(value, destinationType);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException(string.Format("Cannot convert {0} '{1}' to {2}: {3}", attributeMetadata.DisplayName.UserLocalizedLabel.Label, value, destinationType.Name, ex.Message), ex);
                }
            }
            else
            {
                throw new ArgumentException(string.Format("Cannot convert {0} '{1}' to {2}.", attributeMetadata.DisplayName.UserLocalizedLabel.Label, value, destinationType.Name));
            }

            return(convertedValue);
        }
예제 #2
0
        /// <summary>
        /// Creates a Dynamics365Field instance from the field's AttributeMetadata.
        /// </summary>
        /// <param name="attributeMetadata">The AttributeMetadata.</param>
        /// <returns>A Dynamics365Field.</returns>
        public static Dynamics365Field CreateFromMetadata(AttributeMetadata attributeMetadata, Dynamics365Connection connection)
        {
            Dynamics365Field         field     = null;
            Dynamics365TypeConverter converter = new Dynamics365TypeConverter(attributeMetadata);

            if (converter.CanConvertTo(converter.Dynamics365Type) && attributeMetadata.DisplayName.LocalizedLabels.Count > 0)
            {
                field = new Dynamics365Field()
                {
                    LogicalName       = attributeMetadata.LogicalName,
                    DisplayName       = attributeMetadata.DisplayName.UserLocalizedLabel.Label,
                    EntityLogicalName = attributeMetadata.EntityLogicalName,
                    IsPrimaryId       = (bool)attributeMetadata.IsPrimaryId,
                    CanCreate         = (bool)attributeMetadata.IsValidForCreate,
                    CanUpdate         = (bool)attributeMetadata.IsValidForUpdate,
                    Targets           = attributeMetadata is LookupAttributeMetadata ? ((LookupAttributeMetadata)attributeMetadata).Targets : default(string[])
                };

                // see https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/configure-activity-feeds
                if ((field.EntityLogicalName == "post" || field.EntityLogicalName == "postfollow") && attributeMetadata is LookupAttributeMetadata && field.Targets?.Length == 0)
                {
                    using (OrganizationServiceProxy proxy = connection.OrganizationServiceProxy)
                    {
                        ConnectionCache cache    = new ConnectionCache(connection);
                        string          cacheKey = string.Format("GetPostConfigurations");
                        string[]        targets  = (string[])cache[cacheKey];

                        if (targets == null)
                        {
                            const string ENTITY_NAME_LOGICAL_NAME = "msdyn_entityname";

                            FilterExpression filter = new FilterExpression();
                            filter.AddCondition("statecode", ConditionOperator.Equal, 0);
                            filter.AddCondition("statuscode", ConditionOperator.Equal, 1);

                            QueryExpression query = new QueryExpression()
                            {
                                EntityName = "msdyn_postconfig",
                                ColumnSet  = new ColumnSet(ENTITY_NAME_LOGICAL_NAME),
                                Criteria   = filter,
                            };

                            EntityCollection response = proxy.RetrieveMultiple(query);
                            field.Targets = new string[response.Entities.Count];

                            for (int entityIndex = 0; entityIndex < response.Entities.Count; entityIndex++)
                            {
                                field.Targets[entityIndex] = response.Entities[entityIndex].Attributes[ENTITY_NAME_LOGICAL_NAME].ToString();
                            }

                            cache[cacheKey] = field.Targets;
                        }
                    }
                }
            }

            return(field);
        }