private static object ProcessAliasedValue(CrmAttributeStore attribute, object attributeValue)
        {
            var aliasedValue = (JObject)attribute.AttributeValue;

            if (aliasedValue["Value"].Any())
            {
                try
                {
                    var entValue = aliasedValue["Value"];

                    if (entValue["Name"] != null)
                    {
                        var aliasedValueLookup = new AliasedValue((string)aliasedValue["EntityLogicalName"], (string)aliasedValue["AttributeLogicalName"], (string)entValue["Name"]);
                        attributeValue = aliasedValueLookup;
                    }
                    else if (entValue["Value"] != null)
                    {
                        var aliasedValueLookup = new AliasedValue((string)aliasedValue["EntityLogicalName"], (string)aliasedValue["AttributeLogicalName"], (string)entValue["Value"]);
                        attributeValue = aliasedValueLookup;
                    }
                }
                catch (Exception ex)
                {
                    throw new ConfigurationException($"Unsupported type used for alias {(string)aliasedValue["EntityLogicalName"]}, only EntityReference and string are supported, error:{ex.ToString()}", ex);
                }
            }
            else
            {
                var aliasedValueLookup = new AliasedValue((string)aliasedValue["EntityLogicalName"], (string)aliasedValue["AttributeLogicalName"], (string)aliasedValue["Value"]);
                attributeValue = aliasedValueLookup;
            }

            return(attributeValue);
        }
        public void CrmAttributeStore()
        {
            FluentActions.Invoking(() => systemUnderTest = new CrmAttributeStore())
            .Should()
            .NotThrow();

            Assert.IsNull(systemUnderTest.AttributeName);
            Assert.IsNull(systemUnderTest.AttributeValue);
            Assert.IsNull(systemUnderTest.AttributeType);
        }
        public void CrmAttributeStoreWithParameter()
        {
            var attribute = new KeyValuePair <string, object>("contactid", Guid.NewGuid());

            FluentActions.Invoking(() => systemUnderTest = new CrmAttributeStore(attribute))
            .Should()
            .NotThrow();

            Assert.AreEqual(attribute.Key, systemUnderTest.AttributeName);
            Assert.AreEqual(attribute.Value.ToString(), systemUnderTest.AttributeValue.ToString());
        }
Exemple #4
0
        public void GetAttributeValueForCsvAliasedValue()
        {
            var input     = new AliasedValue("contact", "contactid", Guid.NewGuid().ToString());
            var attribute = new CrmAttributeStore(new KeyValuePair <string, object>("firstname", input))
            {
                AttributeType = "Microsoft.Xrm.Sdk.AliasedValue"
            };

            var actual = EntityConverterHelper.GetAttributeValueForCsv(attribute);

            actual.Should().Be(input.Value);
        }
Exemple #5
0
        public void GetAttributeValueForCsvByte()
        {
            byte[] input     = new byte[5];
            var    attribute = new CrmAttributeStore(new KeyValuePair <string, object>("firstname", input))
            {
                AttributeType = "System.Byte[]"
            };

            FluentActions.Invoking(() => EntityConverterHelper.GetAttributeValueForCsv(attribute))
            .Should()
            .Throw <ConfigurationException>();
        }
Exemple #6
0
        public void GetAttributeValueForCsvMoney()
        {
            var input     = new Money(new decimal(12.00));
            var attribute = new CrmAttributeStore(new KeyValuePair <string, object>("firstname", input))
            {
                AttributeType = "Microsoft.Xrm.Sdk.Money"
            };

            var actual = EntityConverterHelper.GetAttributeValueForCsv(attribute);

            actual.Should().Be(input.Value);
        }
Exemple #7
0
        public void GetAttributeValueForCsvOptionSetValue()
        {
            var input = new OptionSetValue(12);
            CrmAttributeStore attribute = new CrmAttributeStore(new KeyValuePair <string, object>("firstname", input))
            {
                AttributeType = "Microsoft.Xrm.Sdk.OptionSetValue"
            };

            var actual = EntityConverterHelper.GetAttributeValueForCsv(attribute);

            actual.ToString().Should().Be(input.Value.ToString(CultureInfo.InvariantCulture));
        }
Exemple #8
0
        public void GetAttributeValueForCsvEntityReference()
        {
            var input = new EntityReference("contact", Guid.NewGuid());
            CrmAttributeStore attribute = new CrmAttributeStore(new KeyValuePair <string, object>("firstname", input))
            {
                AttributeType = "Microsoft.Xrm.Sdk.EntityReference"
            };

            var actual = EntityConverterHelper.GetAttributeValueForCsv(attribute);

            actual.ToString().Should().Be(input.Id.ToString());
        }
Exemple #9
0
        public void GetAttributeValueForCsvEntityCollection()
        {
            var list = new List <Entity>
            {
                new Entity("account", Guid.NewGuid())
            };
            EntityCollection input = new EntityCollection(list);
            var attribute          = new CrmAttributeStore(new KeyValuePair <string, object>("firstname", input))
            {
                AttributeType = "Microsoft.Xrm.Sdk.EntityCollection"
            };

            FluentActions.Invoking(() => EntityConverterHelper.GetAttributeValueForCsv(attribute))
            .Should()
            .Throw <ConfigurationException>();
        }
Exemple #10
0
        public void GetAttributeValueForCsvOptionSetValueCollection()
        {
            var list = new List <OptionSetValue>
            {
                new OptionSetValue(12),
                new OptionSetValue(3)
            };
            var input     = new OptionSetValueCollection(list);
            var attribute = new CrmAttributeStore(new KeyValuePair <string, object>("firstname", input))
            {
                AttributeType = "Microsoft.Xrm.Sdk.OptionSetValueCollection"
            };

            var actual = EntityConverterHelper.GetAttributeValueForCsv(attribute);

            actual.Should().Be("12|3");
        }
        public static object GetAttributeValueForCsv(CrmAttributeStore attribute)
        {
            attribute.ThrowArgumentNullExceptionIfNull(nameof(attribute));

            if (attribute.AttributeValue == null)
            {
                return(null);
            }

            object attributeValue = string.Empty;

            switch (attribute.AttributeType)
            {
            case "Microsoft.Xrm.Sdk.EntityReference":
                var entRef = (EntityReference)attribute.AttributeValue;
                attributeValue = entRef.Id;
                break;

            case "Microsoft.Xrm.Sdk.OptionSetValue":
                var optSetValue = (OptionSetValue)attribute.AttributeValue;
                attributeValue = optSetValue.Value;
                break;

            case "Microsoft.Xrm.Sdk.OptionSetValueCollection":
                var optionSetValues = (OptionSetValueCollection)attribute.AttributeValue;

                var s = new StringBuilder();

                optionSetValues.ToList().ForEach(x =>
                {
                    if (s.Length == 0)
                    {
                        s.Append(x.Value.ToString(CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        s.Append("|" + x.Value.ToString(CultureInfo.InvariantCulture));
                    }
                });

                attributeValue = s.ToString();
                break;

            case "Microsoft.Xrm.Sdk.Money":
                var money = (Money)attribute.AttributeValue;
                attributeValue = money.Value;
                break;

            case "System.Byte[]":
            case "Microsoft.Xrm.Sdk.EntityCollection":
                throw new ConfigurationException($"Not supported attribute type {attribute.AttributeType}");

            case "Microsoft.Xrm.Sdk.AliasedValue":
                // at the monent alsiaed value would be always entity ref - if other types will be used, we need some proper logic here!
                var aliasedValue = (AliasedValue)attribute.AttributeValue;
                if (aliasedValue.Value is EntityReference entref)
                {
                    attributeValue = entref.Name;
                }
                else if (aliasedValue.Value is OptionSetValue optSet)
                {
                    attributeValue = optSet.Value;
                }
                else
                {
                    attributeValue = aliasedValue.Value;
                }

                break;

            default:
                attributeValue = attribute.AttributeValue;
                break;
            }

            return(attributeValue);
        }