Ejemplo n.º 1
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="data">The data.</param>
        public override void SetValue(string value, params string[] data)
        {
            if (this.Adaptee is StateProperty)
            {
                ((StateProperty)this.Adaptee).Value = value;
                return;
            }

            if (this.Adaptee is StringProperty)
            {
                ((StringProperty)this.Adaptee).Value = value;
                return;
            }

            if (this.Adaptee is UniqueIdentifierProperty)
            {
                ID idValue;
                if (ID.TryParse(value, out idValue))
                {
                    var identifier = new UniqueIdentifier {
                        Value = idValue.Guid
                    };
                    ((UniqueIdentifierProperty)this.Adaptee).Value = identifier;
                }

                return;
            }

            if (this.Adaptee is StatusProperty)
            {
                int intValue;
                if (int.TryParse(value, out intValue))
                {
                    var status = new Status {
                        Value = intValue
                    };
                    ((StatusProperty)this.Adaptee).Value = status;
                }

                return;
            }

            if (this.Adaptee is PicklistProperty)
            {
                string   picklistValue = value;
                Picklist list          = null;
                int      intValue;

                if (!string.IsNullOrEmpty(picklistValue) && int.TryParse(picklistValue, out intValue))
                {
                    list = new Picklist {
                        Value = intValue
                    };
                }
                else
                {
                    picklistValue = null;
                }

                if (string.IsNullOrEmpty(picklistValue))
                {
                    list = new Picklist
                    {
                        IsNull          = true,
                        IsNullSpecified = true
                    };
                }

                ((PicklistProperty)this.Adaptee).Value = list;
                return;
            }

            if (this.Adaptee is OwnerProperty)
            {
                ID idValue;
                if (ID.TryParse(value, out idValue))
                {
                    var owner = new Owner {
                        Value = idValue.Guid, type = data[0]
                    };
                    ((OwnerProperty)this.Adaptee).Value = owner;
                }

                return;
            }

            if (this.Adaptee is LookupProperty)
            {
                ID idValue;
                if (ID.TryParse(value, out idValue))
                {
                    var lookup = new Lookup {
                        Value = idValue.Guid, type = data[0]
                    };
                    ((LookupProperty)this.Adaptee).Value = lookup;
                }

                return;
            }

            if (this.Adaptee is KeyProperty)
            {
                ID idValue;
                if (ID.TryParse(value, out idValue))
                {
                    var key = new Key {
                        Value = idValue.Guid
                    };
                    ((KeyProperty)this.Adaptee).Value = key;
                }

                return;
            }

            if (this.Adaptee is EntityNameReferenceProperty)
            {
                var reference = new EntityNameReference {
                    Value = value
                };
                ((EntityNameReferenceProperty)this.Adaptee).Value = reference;
                return;
            }

            if (this.Adaptee is DynamicEntityArrayProperty)
            {
                var dynamycProperty = (DynamicEntityArrayProperty)this.Adaptee;

                dynamycProperty.Value = new[]
                {
                    new DynamicEntity
                    {
                        Name       = "activityparty",
                        Properties = new[]
                        {
                            new LookupProperty
                            {
                                Name  = "partyid",
                                Value = new Lookup
                                {
                                    Value = new Guid(value),
                                    type  = data[0]
                                }
                            }
                        }
                    }
                };
            }

            if (this.Adaptee is CustomerProperty)
            {
                ID idValue;
                if (ID.TryParse(value, out idValue))
                {
                    var customer = new Customer {
                        Value = idValue.Guid, type = data[0]
                    };
                    ((CustomerProperty)this.Adaptee).Value = customer;
                }

                return;
            }

            if (this.Adaptee is CrmNumberProperty)
            {
                int intValue;
                if (int.TryParse(value, out intValue))
                {
                    var number = new CrmNumber {
                        Value = intValue
                    };
                    ((CrmNumberProperty)this.Adaptee).Value = number;
                }

                return;
            }

            if (this.Adaptee is CrmMoneyProperty)
            {
                decimal decimalValue;
                if (decimal.TryParse(value, out decimalValue))
                {
                    var money = new CrmMoney {
                        Value = decimalValue
                    };
                    ((CrmMoneyProperty)this.Adaptee).Value = money;
                }

                return;
            }

            if (this.Adaptee is CrmFloatProperty)
            {
                float decimalValue;
                if (float.TryParse(value, out decimalValue))
                {
                    var crmFloat = new CrmFloat {
                        Value = decimalValue
                    };
                    ((CrmFloatProperty)this.Adaptee).Value = crmFloat;
                }

                return;
            }

            if (this.Adaptee is CrmDecimalProperty)
            {
                decimal decimalValue;
                if (decimal.TryParse(value, out decimalValue))
                {
                    var crmDecimal = new CrmDecimal {
                        Value = decimalValue
                    };
                    ((CrmDecimalProperty)this.Adaptee).Value = crmDecimal;
                }

                return;
            }

            if (this.Adaptee is CrmDateTimeProperty)
            {
                DateTime parsedValue;
                if (value.TryParseDateTime(out parsedValue))
                {
                    ((CrmDateTimeProperty)this.Adaptee).Value = new CrmDateTime {
                        Value = parsedValue.ToCrmDateTimeString()
                    };
                }
            }

            if (this.Adaptee is CrmBooleanProperty)
            {
                var boolValue = MainUtil.GetBool(value, false);
                var crmBool   = new CrmBoolean {
                    Value = boolValue
                };
                ((CrmBooleanProperty)this.Adaptee).Value = crmBool;
            }
        }
Ejemplo n.º 2
0
    /// <summary>
    /// Returns the default string value
    /// </summary>
    /// <param name="crmEntity">Entity</param>
    /// <param name="propertyName">Attribute Name</param>
    /// <returns>Attribute value(String)</returns>
    public static string GetDefaultFieldValue(this DynamicEntity crmEntity, string propertyName)
    {
        if (crmEntity.Properties.Contains(propertyName))
        {
            string ret  = string.Empty;
            string type = null;


            try
            {
                //TODO: Google "CrmTypes"
                type = crmEntity.Properties[propertyName].GetType().ToString();
            }
            catch (Exception exception)
            {
                type = exception.Message;
                ret  = exception.Message;
                return(null);
            }

            if (type == "Microsoft.Crm.Sdk.Owner")
            {
                Owner owner = (Owner)crmEntity[propertyName];
                return(owner.name);
            }
            if (type == "Microsoft.Crm.Sdk.Customer")
            {
                Customer customer = (Customer)crmEntity[propertyName];
                return(customer.name);
            }
            if (type == "Microsoft.Crm.Sdk.CrmDateTime")
            {
                CrmDateTimeProperty crmDataTime = new CrmDateTimeProperty(propertyName, (CrmDateTime)crmEntity.Properties[propertyName]);
                return(crmDataTime.Value.date);
            }
            if (type == "Microsoft.Crm.Sdk.CrmBoolean")
            {
                CrmBooleanProperty crmBooleanProperty = new CrmBooleanProperty(propertyName, (CrmBoolean)crmEntity.Properties[propertyName]);
                ret = crmBooleanProperty.Value.Value.ToString();
                return(ret);
            }
            if (type == "System.String")
            {
                return(crmEntity.Properties[propertyName].ToString());
            }
            if (type == "Microsoft.Crm.Sdk.Lookup")
            {
                Lookup crmLookup = (Lookup)crmEntity[propertyName];
                return(crmLookup.name);
            }
            if (type == "Microsoft.Crm.Sdk.Picklist")
            {
                Picklist crmPicklist = (Picklist)crmEntity[propertyName];
                return(crmPicklist.name);
            }
            if (type == "Microsoft.Crm.Sdk.CrmNumber")
            {
                CrmNumber num = (CrmNumber)crmEntity[propertyName];
                return(num.Value.ToString());
            }
            return(ret);
        }
        else
        {
            return("The Entity doesn't have this property defined");
        }
    }
Ejemplo n.º 3
0
 public static int?Convert(CrmNumber obj)
 {
     return(obj.IsNull == false ? (int?)obj.Value : null);
 }
Ejemplo n.º 4
0
    /// <summary>
    /// Set field value
    /// </summary>
    /// <param name="DynamicEntityObject">DynamicEntity</param>
    /// <param name="service">MetadataService</param>
    /// <param name="AttributeType">Type of attribute</param>
    /// <param name="AttributeName">Attribute name</param>
    /// <param name="AttributeValue1">Attribute first value</param>
    /// <param name="AttributeValue2">Attribute first value</param>
    /// <returns></returns>
    public static bool SetFieldValue(this DynamicEntity DynamicEntityObject, MetadataService service, String AttributeType, string AttributeName, String AttributeValue1, String AttributeValue2)
    {
        //bool ret = false;
        string TypeOfAttribute;    // = null;

        try
        {
            //$ when the
            if (DynamicEntityObject.Properties.Contains(AttributeName))
            {
                TypeOfAttribute = DynamicEntityObject.Properties[AttributeName].GetType().Name;
            }
            else
            {
                //TODO: replace this function with meta data service
                TypeOfAttribute = RetrieveAttributeMetadata(service, DynamicEntityObject.Name, AttributeName);
            }


            if (TypeOfAttribute == null || TypeOfAttribute == "PrimaryKey")
            {
                return(false);
            }
            else if (TypeOfAttribute == "Microsoft.Crm.Sdk.Owner" || TypeOfAttribute == "Owner")
            {
                DynamicEntityObject[AttributeName] = new Lookup(AttributeValue1, new Guid(AttributeValue2));
                return(true);
            }
            else if (TypeOfAttribute == "Microsoft.Crm.Sdk.Customer" || TypeOfAttribute == "Customer")
            {
                DynamicEntityObject[AttributeName] = new Customer(AttributeValue1, new Guid(AttributeValue2));
                return(true);
            }
            else if (TypeOfAttribute == "Microsoft.Crm.Sdk.CrmDateTime" || TypeOfAttribute == "CrmDateTime" || TypeOfAttribute == "Microsoft.Crm.Sdk.Metadata.DateTimeAttributeMetadata")
            {
                DynamicEntityObject[AttributeName] = new CrmDateTime(AttributeValue1);
                return(true);
            }
            else if (TypeOfAttribute == "Microsoft.Crm.Sdk.CrmBoolean" || TypeOfAttribute == "CrmBoolean" || TypeOfAttribute == "Microsoft.Crm.Sdk.Metadata.BooleanAttributeMetadata")
            {
                DynamicEntityObject.Properties[AttributeName] = new CrmBoolean(bool.Parse(AttributeValue1));
                return(true);
            }
            else if (TypeOfAttribute == "Microsoft.Crm.Sdk.String" || TypeOfAttribute == "String" || TypeOfAttribute == "Microsoft.Crm.Sdk.Metadata.StringAttributeMetadata")
            {
                DynamicEntityObject[AttributeName] = (string)AttributeValue1;
                return(true);
            }
            else if (TypeOfAttribute == "Microsoft.Crm.Sdk.Lookup" || TypeOfAttribute == "Lookup" || TypeOfAttribute == "Microsoft.Crm.Sdk.Metadata.LookupAttributeMetadata")
            {
                DynamicEntityObject[AttributeName] = new Lookup(AttributeValue1, new Guid(AttributeValue2));
                return(true);
            }
            else if (TypeOfAttribute == "Microsoft.Crm.Sdk.Picklist" || TypeOfAttribute == "Picklist" || TypeOfAttribute == "Microsoft.Crm.Sdk.Metadata.PicklistAttributeMetadata")
            {
                DynamicEntityObject[AttributeName] = new Picklist(int.Parse(AttributeValue1));
                return(true);
            }
            else if (TypeOfAttribute == "Microsoft.Crm.Sdk.CrmNumber" || TypeOfAttribute == "CrmNumber" || TypeOfAttribute == "Integer" || TypeOfAttribute == "Microsoft.Crm.Sdk.Metadata.IntegerAttributeMetadata")
            {
                DynamicEntityObject[AttributeName] = new CrmNumber(int.Parse(AttributeValue1));
                return(true);
            }
        }
        catch (SoapException sopEx)
        {
            throw;
        }

        return(false);
    }