コード例 #1
0
        private void SetEntityAttributes(Entity entity, IDictionary values)
        {
            var metadata = _crmDataContext.RetrieveEntity(entity.LogicalName, EntityFilters.Attributes);

            foreach (DictionaryEntry value in values)
            {
                var attributeName = value.Key.ToString().TrimStart('[').TrimEnd(']');

                if (string.Equals(attributeName, "EntityName", StringComparison.InvariantCulture))
                {
                    continue;
                }

                var attributeType = metadata.Attributes.Single(a => a.LogicalName == attributeName).AttributeType;

                if (!(value.Value is EntityReference) && (
                        attributeType == AttributeTypeCode.Customer ||
                        attributeType == AttributeTypeCode.Lookup ||
                        attributeType == AttributeTypeCode.Owner))
                {
                    // TODO: CRM5 port - figure out how to set an entity reference.
                    throw new NotSupportedException("EntityRefernce attribute types are not supported and cannot be inserted or updated currently.");
                }
                else if (attributeType == AttributeTypeCode.Status ||
                         attributeType == AttributeTypeCode.Picklist)
                {
                    entity.SetAttributeValue <OptionSetValue>(attributeName, value.Value);
                }
                else if (attributeType == AttributeTypeCode.Money)
                {
                    entity.SetAttributeValue <Money>(attributeName, value.Value);
                }
                else
                {
                    entity[attributeName] = value.Value;
                }
            }
        }
コード例 #2
0
        protected virtual void SetEntityAttributes(Entity entity, IDictionary values)
        {
            var metadata = _crmDataContext.RetrieveEntity(entity.LogicalName, EntityFilters.Attributes);

            foreach (DictionaryEntry value in values)
            {
                var attributeName = value.Key.ToString().TrimStart('[').TrimEnd(']');

                if (string.Equals(attributeName, "EntityName", StringComparison.InvariantCulture))
                {
                    continue;
                }

                AttributeTypeCode?attributeType;
                try
                {
                    var attribute = metadata.Attributes.FirstOrDefault(a => a.LogicalName == attributeName);

                    if (attribute == null)                     // ignore subgrid, iframe or web resources
                    {
                        ADXTrace.Instance.TraceInfo(TraceCategory.Application, "No value set. Key '{0}' is not a name of an attribute on entity type {1}. This is likely a subgrid, iframe, or web resource.", attributeName, entity.LogicalName);
                        continue;
                    }

                    attributeType = attribute.AttributeType;
                }
                catch (Exception)
                {
                    throw new Exception("{0} could not be found in entity type {1}".FormatWith(attributeName, entity.LogicalName));
                }

                if (!(value.Value is EntityReference) && (
                        attributeType == AttributeTypeCode.Customer ||
                        attributeType == AttributeTypeCode.Lookup ||
                        attributeType == AttributeTypeCode.Owner))
                {
                    Guid id;

                    if (value.Value == null)
                    {
                        entity.SetAttributeValue <EntityReference>(attributeName, null);

                        continue;
                    }

                    if (!Guid.TryParse(value.Value.ToString(), out id))
                    {
                        throw new ArgumentException("value is not of type Guid");
                    }

                    var entityReference = new EntityReference(entity.LogicalName, id);

                    entity.SetAttributeValue <EntityReference>(attributeName, entityReference);
                }
                else if (attributeType == AttributeTypeCode.Status)
                {
                    entity.SetAttributeValue <OptionSetValue>(attributeName, value.Value);
                }
                else if (attributeType == AttributeTypeCode.Picklist)
                {
                    // determine if a multiselect picklist values should be saved
                    var picklistvaluesfield = metadata.Attributes.FirstOrDefault(a => a.LogicalName == string.Format("{0}selectedvalues", attributeName));

                    if (picklistvaluesfield == null)
                    {
                        entity.SetAttributeValue <OptionSetValue>(attributeName, value.Value);
                    }
                    else
                    {
                        entity[picklistvaluesfield.LogicalName] = value.Value;
                    }
                }
                else if (attributeType == AttributeTypeCode.Money)
                {
                    entity.SetAttributeValue <Money>(attributeName, value.Value);
                }
                else
                {
                    entity[attributeName] = value.Value;
                }
            }
        }