public static CRMRecord ToCRMRecord(this Entity entity)
        {
            CRMRecord crmrecord = null;

            if (entity == null)
            {
                return(crmrecord);
            }

            List <string> attributesToExclude = Util.GetCRMAttributesToExclude();

            List <CRMAttribute> crmAttributes = new List <CRMAttribute>();

            foreach (var attribute in entity.Attributes)
            {
                if (attributesToExclude.Contains(attribute.Key))
                {
                    continue;
                }

                CRMAttribute crmAttribute = GetCRMAttribute(attribute);
                crmAttributes.Add(crmAttribute);
            }

            crmrecord = new CRMRecord {
                LogicalName = entity.LogicalName, Id = entity.Id, CRMAttributes = crmAttributes
            };

            return(crmrecord);
        }
Exemplo n.º 2
0
        private static object GetEntityAttribute(CRMAttribute crmAttribute, Dictionary <Guid, Guid> sourceTargetIdMappings)
        {
            object val = null;

            if (crmAttribute.Value != null && crmAttribute.AttributeType != null)
            {
                switch (crmAttribute.AttributeType.ToLower())
                {
                case "entityreference":

                    Guid sourceId = new Guid(crmAttribute.Value);
                    if (sourceTargetIdMappings != null && sourceTargetIdMappings.ContainsKey(sourceId))
                    {
                        Guid targetId = sourceTargetIdMappings[sourceId];
                        val = new EntityReference(crmAttribute.LookupEntity, targetId);
                    }
                    else
                    {
                        val = new EntityReference(crmAttribute.LookupEntity, sourceId);
                    }


                    break;

                case "datetime":
                    val = DateTime.Parse(crmAttribute.Value);
                    break;

                case "boolean":
                    val = bool.Parse(crmAttribute.Value);
                    break;

                case "string":
                    val = crmAttribute.Value;
                    break;

                case "int32":
                case "integer":
                    val = int.Parse(crmAttribute.Value);
                    break;

                case "decimal":
                    val = decimal.Parse(crmAttribute.Value);
                    break;

                case "money":
                    val = new Money(Decimal.Parse(crmAttribute.Value));
                    break;

                case "double":
                    val = double.Parse(crmAttribute.Value);
                    break;

                case "optionsetvalue":
                    val = new OptionSetValue(int.Parse(crmAttribute.Value));
                    break;
                }
            }
            return(val);
        }
        public static CRMAttribute GetCRMAttribute(KeyValuePair <string, object> attribute)
        {
            string attValue     = null;
            string lookupEntity = null;

            Object attObj = attribute.Value;

            string attType = (attObj != null) ? attObj.GetType().Name : null;

            if (attType != null)
            {
                switch (attType.ToLower())
                {
                case "entityreference":
                    EntityReference er = attObj as EntityReference;
                    lookupEntity = er.LogicalName;
                    attValue     = er.Id.ToString();
                    break;

                case "datetime":
                    DateTime dt = (DateTime)attObj;
                    attValue = dt.ToString("M/d/yyyy hh:mm:ss tt",
                                           CultureInfo.InvariantCulture);
                    break;

                case "money":
                    Money money = (Money)attObj;
                    attValue = money.Value.ToString();
                    break;

                case "optionsetvalue":
                    OptionSetValue osv = (OptionSetValue)attObj;
                    attValue = osv.Value.ToString();
                    break;

                default:
                    attValue = attObj.ToString();
                    break;
                }
            }

            CRMAttribute crmAttribute = new CRMAttribute
            {
                LogicalName   = attribute.Key,
                Value         = attValue,
                AttributeType = attType,
                LookupEntity  = lookupEntity
            };


            return(crmAttribute);
        }
        private void btnGetState_Click(object sender, RoutedEventArgs e)
        {
            CRMEntityMetadata emd = cbEntities.SelectedItem as CRMEntityMetadata;
            CRMAttribute      att = cbAttributes.SelectedItem as CRMAttribute;

            CRMPicklist pl = new CRMPicklist();

            pl.EntityLogicalName = emd.LogicalName;



            cbState.ItemsSource = functions.CRMGetStateStatus(pl).Picklist;
        }
        private void btnGetPicklist_Click(object sender, RoutedEventArgs e)
        {
            CRMEntityMetadata emd = cbEntities.SelectedItem as CRMEntityMetadata;
            CRMAttribute      att = cbAttributes.SelectedItem as CRMAttribute;

            CRMPicklist pl = new CRMPicklist();

            pl.AttributeLogicalName = att.LogicalName;
            pl.EntityLogicalName    = emd.LogicalName;

            CRMPicklist cp = functions.CRMGetPicklist(pl);

            cbOptions.ItemsSource = cp.Picklist.OrderByDescending(p => p.PicklistParentValue).OrderBy(p => p.PicklistValue);
        }
Exemplo n.º 6
0
        public static ColumnSet GetColumnSetByFields(params Expression <Func <T, Object> >[] fields)
        {
            ColumnSet columns = new ColumnSet();

            foreach (var obj in fields)
            {
                // TODO: magic?
                CRMAttribute attr = GetAttributeFromExpression(obj?.Body, typeof(CRMAttribute)) as CRMAttribute;

                if (attr != null)
                {
                    columns.AddColumn(attr.FieldName);
                }
            }

            return(columns);
        }
Exemplo n.º 7
0
        public static EntityReference ToEntityReference(this CRMRecord crmRecord)
        {
            string primaryAttributeName = crmRecord.LogicalName.StartsWith("uii") ? "uii_name" : "msdyusd_name";


            CRMAttribute primaryNameAtt        = (crmRecord.CRMAttributes != null) ? crmRecord.CRMAttributes.Where(x => x.LogicalName == primaryAttributeName).FirstOrDefault() : null;
            string       primaryAttributeValue = (primaryNameAtt != null && primaryNameAtt.Value != null) ? primaryNameAtt.Value.ToString() : null;

            EntityReference entityReference = new EntityReference
            {
                Id          = crmRecord.Id,
                LogicalName = crmRecord.LogicalName,
                Name        = primaryAttributeValue
            };

            return(entityReference);
        }
        private Property SetGetEntityAttributeProperties(Property prop, CRMAttribute task)
        {
            switch (prop.Name.ToLower())
            {
            case "attributelogicalname":
                prop.Value = task.LogicalName;
                break;

            case "attributeschemaname":
                prop.Value = task.SchemaName;
                break;

            case "attributedisplayname":
                prop.Value = task.DisplayName;
                break;

            case "attributetype":
                prop.Value = task.AttributeType;
                break;

            case "attributedescription":
                prop.Value = task.Description;
                break;

            case "attributerequiredlevel":
                prop.Value = task.RequiredLevel;
                break;

            case "attributeisvalidforcreate":
                prop.Value = task.IsValidForCreate;
                break;

            case "attributeisvalidforread":
                prop.Value = task.IsValidForRead;
                break;

            case "attributeisvalidforupdate":
                prop.Value = task.IsValidForUpdate;
                break;
            }

            return(prop);
        }
Exemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="image">WebHook: Target, PreImage, or PostImage</param>
        /// <param name="model">Model with CRM Attributes for metadata mapping</param>
        /// <returns></returns>
        public static T MapToModel(JObject image, T model)
        {
            List <AttributeModel> attributes = image["Attributes"].ToObject <List <AttributeModel> >();

            foreach (AttributeModel attr in attributes)
            {
                List <PropertyInfo> props = GetFieldByFieldName(attr.key.ToString());

                foreach (PropertyInfo prop in props)
                {
                    if (prop == null)
                    {
                        continue;
                    }

                    // get custom attribute
                    CRMAttribute crmAttr = prop.GetCustomAttributes().SingleOrDefault(x => x.GetType() == typeof(CRMAttribute)) as CRMAttribute;

                    // manually map first
                    if (crmAttr.CustomFieldMap != null)
                    {
                        if (CustomMappingMethod == null)
                        {
                            throw new InvalidOperationException($"The custom mapping method for '{crmAttr.CustomFieldMap}' has not been set.");
                        }

                        CustomMappingMethod(model, crmAttr.CustomFieldMap, prop, attr.value);
                        continue;
                    }

                    // Simple Fields: Single Line of Text, Option Set, Two Options, Image, Whole Number, Floating Point Number, Decimal Number, Currency, Multiple Lines of Text, Date and Time, Lookup

                    // automap properties
                    if (attr.value.GetType() == typeof(String))
                    {
                        // string
                        if (prop.PropertyType == typeof(Guid?) || prop.PropertyType == typeof(Guid))
                        {
                            prop.SetValue(model, new Guid(attr.value.ToString()));
                        }
                        else
                        {
                            prop.SetValue(model, attr.value);
                        }
                    }
                    else if (attr.value.GetType() == typeof(int?) || attr.value.GetType() == typeof(int))
                    {
                        // int
                        prop.SetValue(model, (int)attr.value);
                    }
                    else if (attr.value.GetType() == typeof(bool?) || attr.value.GetType() == typeof(bool))
                    {
                        // bool
                        prop.SetValue(model, (bool)attr.value);
                    }
                    else if (attr.value.GetType() == typeof(DateTime?) || attr.value.GetType() == typeof(DateTime))
                    {
                        // datetime
                        prop.SetValue(model, (DateTime)attr.value);
                    }
                    else if (attr.value.GetType() == typeof(JObject))
                    {
                        // copmlex data types
                        JObject json        = ((JObject)attr.value);
                        string  dataType    = (string)json["__type"];
                        Type    crmDataType = CRMDataType.Dictionary[dataType];

                        // entity reference
                        if (crmDataType == typeof(EntityReference))
                        {
                            // EntityReference
                            if (prop.PropertyType == typeof(String))
                            {
                                prop.SetValue(model, (String)json["Name"]);
                            }
                            else if (prop.PropertyType == typeof(Guid) || prop.PropertyType == typeof(Guid?))
                            {
                                prop.SetValue(model, (Guid)json["Id"]);
                            }
                        }
                        else if (crmDataType == typeof(OptionSetValue))
                        {
                            string test = "";
                        }
                    }
                } /* end foreach */
            }

            return(model);
        }