// Field Value /// <summary> /// Returns the string property value /// </summary> /// <param name="dynamicEntityInstance">Entity</param> /// <param name="entityPropertyLogicalName">Attribute name</param> /// <returns>Attribute Value(String)</returns> public static string GetPropertyValue(this DynamicEntity dynamicEntityInstance, string entityPropertyLogicalName) { string returnValue = ""; string propertyType = null; try { propertyType = dynamicEntityInstance.Properties[entityPropertyLogicalName].GetType().ToString(); } catch (Exception e) { propertyType = e.Message; returnValue = e.Message; return(null); } if (propertyType == "Microsoft.Crm.Sdk.Owner") { Owner ownerType = (Owner)dynamicEntityInstance[entityPropertyLogicalName]; return(ownerType.name); } else if (propertyType == "Microsoft.Crm.Sdk.Customer") { Customer customerType = (Customer)dynamicEntityInstance[entityPropertyLogicalName]; return(customerType.name); } else if (propertyType == "Microsoft.Crm.Sdk.CrmDateTime") { CrmDateTimeProperty crmDataTimeType = new CrmDateTimeProperty(entityPropertyLogicalName, (CrmDateTime)dynamicEntityInstance.Properties[entityPropertyLogicalName]); return(crmDataTimeType.Value.date); } else if (propertyType == "Microsoft.Crm.Sdk.CrmBoolean") { CrmBooleanProperty crmBooleanProperty = new CrmBooleanProperty(entityPropertyLogicalName, (CrmBoolean)dynamicEntityInstance.Properties[entityPropertyLogicalName]); returnValue = crmBooleanProperty.Value.Value.ToString(); } else if (propertyType == "System.String") { return(dynamicEntityInstance.Properties[entityPropertyLogicalName].ToString()); } else if (propertyType == "Microsoft.Crm.Sdk.Lookup") { Lookup lookupType = (Lookup)dynamicEntityInstance[entityPropertyLogicalName]; return(lookupType.name); } else if (propertyType == "Microsoft.Crm.Sdk.Picklist") { Picklist picklistType = (Picklist)dynamicEntityInstance[entityPropertyLogicalName]; return(picklistType.name); } return(returnValue); }
/// <summary> /// Adds the specified name. /// </summary> /// <param name="name">The name.</param> /// <param name="type">The type.</param> /// <param name="value">The value.</param> /// <param name="data">The data.</param> public ICrmAttribute Create(string name, CrmAttributeType type, string value, params string[] data) { Property property = null; switch (type) { case CrmAttributeType.Boolean: property = new CrmBooleanProperty(); break; case CrmAttributeType.Customer: property = new CustomerProperty(); break; case CrmAttributeType.DateTime: property = new CrmDateTimeProperty(); break; case CrmAttributeType.Decimal: property = new CrmDecimalProperty(); break; case CrmAttributeType.Float: property = new CrmFloatProperty(); break; case CrmAttributeType.Integer: property = new CrmNumberProperty(); break; case CrmAttributeType.Lookup: property = new LookupProperty(); break; case CrmAttributeType.Memo: property = new StringProperty(); break; case CrmAttributeType.Money: property = new CrmMoneyProperty(); break; case CrmAttributeType.Owner: property = new OwnerProperty(); break; case CrmAttributeType.Picklist: property = new PicklistProperty(); break; case CrmAttributeType.State: property = new StateProperty(); break; case CrmAttributeType.Status: property = new StatusProperty(); break; case CrmAttributeType.String: property = new StringProperty(); break; case CrmAttributeType.UniqueIdentifier: property = new UniqueIdentifierProperty(); break; case CrmAttributeType.PartyList: property = new DynamicEntityArrayProperty(); break; case CrmAttributeType.Virtual: case CrmAttributeType.CalendarRules: case CrmAttributeType.Internal: case CrmAttributeType.PrimaryKey: break; } this.Add(property); var crmAttributeAdapter = new CrmAttributeAdapter(property) { Name = name }; crmAttributeAdapter.SetValue(value, data); return(crmAttributeAdapter); }
private string GetValueFromProperty(Microsoft.Crm.Sdk.Property inputProperty) { Type propType = inputProperty.GetType(); string propValue = string.Empty; if (propType == typeof(Microsoft.Crm.Sdk.StringProperty)) { propValue = ((Microsoft.Crm.Sdk.StringProperty)inputProperty).Value; } else if (propType == typeof(Microsoft.Crm.Sdk.CustomerProperty)) { propValue = ((Microsoft.Crm.Sdk.CustomerProperty)inputProperty).Value.Value.ToString(); } else if (propType == typeof(Microsoft.Crm.Sdk.CrmBooleanProperty)) { propValue = ((Microsoft.Crm.Sdk.CrmBooleanProperty)inputProperty).Value.Value.ToString(); } else if (propType == typeof(Microsoft.Crm.Sdk.CrmMoneyProperty)) { propValue = ((Microsoft.Crm.Sdk.CrmMoneyProperty)inputProperty).Value.Value.ToString(); } else if (propType == typeof(Microsoft.Crm.Sdk.CrmDateTimeProperty)) { CrmDateTimeProperty dtProperty = (Microsoft.Crm.Sdk.CrmDateTimeProperty)inputProperty; if (dtProperty != null && dtProperty.Value != null) { propValue = dtProperty.Value.Value; } } else if (propType == typeof(Microsoft.Crm.Sdk.CrmDecimalProperty)) { propValue = ((Microsoft.Crm.Sdk.CrmDecimalProperty)inputProperty).Value.Value.ToString(); } else if (propType == typeof(Microsoft.Crm.Sdk.CrmFloatProperty)) { propValue = ((Microsoft.Crm.Sdk.CrmFloatProperty)inputProperty).Value.Value.ToString(); } else if (propType == typeof(Microsoft.Crm.Sdk.CrmNumberProperty)) { propValue = ((Microsoft.Crm.Sdk.CrmNumberProperty)inputProperty).Value.Value.ToString(); } else if (propType == typeof(Microsoft.Crm.Sdk.LookupProperty)) { if (inputProperty.Name.ToString() != "owningbusinessunit") { propValue = ((Microsoft.Crm.Sdk.LookupProperty)inputProperty).Value.Value.ToString(); } } else if (propType == typeof(Microsoft.Crm.Sdk.OwnerProperty)) { propValue = ((Microsoft.Crm.Sdk.OwnerProperty)inputProperty).Value.Value.ToString(); } else if (propType == typeof(Microsoft.Crm.Sdk.StatusProperty)) { propValue = ((Microsoft.Crm.Sdk.StatusProperty)inputProperty).Value.Value.ToString(); } else if (propType == typeof(Microsoft.Crm.Sdk.PicklistProperty)) { propValue = ((Microsoft.Crm.Sdk.PicklistProperty)inputProperty).Value.name; } else if (propType == typeof(Microsoft.Crm.Sdk.KeyProperty)) { propValue = ((Microsoft.Crm.Sdk.KeyProperty)inputProperty).Value.Value.ToString(); } return(propValue); }
/// <summary> /// Adds the property to collection. /// </summary> /// <param name="propertyName">Name of the property.</param> /// <param name="propertyValue">Value of the property.</param> /// <param name="propertyType">Type of the property.</param> /// <param name="properties">The properties.</param> protected void AddPropertyToCollection(string propertyName, object propertyValue, SupportedTypes propertyType, List <Property> properties) { var propertyStringValue = propertyValue as string; switch (propertyType) { case SupportedTypes.String: var stringProperty = new StringProperty(); stringProperty.Name = propertyName; stringProperty.Value = (string)propertyValue; properties.Add(stringProperty); break; case SupportedTypes.CrmBoolean: if (propertyStringValue != null || (propertyValue is bool)) { var booleanProperty = new CrmBooleanProperty(); booleanProperty.Name = propertyName; booleanProperty.Value = new crm4.webservice.CrmBoolean(); booleanProperty.Value.IsNull = true; if (propertyValue is bool) { booleanProperty.Value.Value = (bool)propertyValue; } else { booleanProperty.Value.Value = MainUtil.GetBool(propertyStringValue, false); } booleanProperty.Value.IsNull = false; properties.Add(booleanProperty); } break; case SupportedTypes.CrmDateTime: if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is DateTime)) { var datetimeProperty = new CrmDateTimeProperty(); datetimeProperty.Name = propertyName; datetimeProperty.Value = new crm4.webservice.CrmDateTime(); datetimeProperty.Value.IsNull = true; if (propertyValue is DateTime) { datetimeProperty.Value.Value = ((DateTime)propertyValue).ToString("yyyy-MM-ddTHH:mm:sszzzz"); datetimeProperty.Value.IsNull = false; } else { DateTime dateTimeValue; if (DateTime.TryParse(propertyStringValue, out dateTimeValue)) { datetimeProperty.Value.Value = dateTimeValue.ToString("yyyy-MM-ddTHH:mm:sszzzz"); datetimeProperty.Value.IsNull = false; } } if (!datetimeProperty.Value.IsNull) { properties.Add(datetimeProperty); } } break; case SupportedTypes.CrmFloat: if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is float)) { var floatProperty = new CrmFloatProperty(); floatProperty.Name = propertyName; floatProperty.Value = new crm4.webservice.CrmFloat(); floatProperty.Value.IsNull = true; if (propertyValue is float) { floatProperty.Value.Value = (float)propertyValue; floatProperty.Value.IsNull = false; } else { float floatValue; if (float.TryParse(propertyStringValue, out floatValue)) { floatProperty.Value.Value = floatValue; floatProperty.Value.IsNull = false; } } if (!floatProperty.Value.IsNull) { properties.Add(floatProperty); } } break; case SupportedTypes.CrmDecimal: if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is decimal)) { var decimalProperty = new CrmDecimalProperty(); decimalProperty.Name = propertyName; decimalProperty.Value = new CrmDecimal(); decimalProperty.Value.IsNull = true; if (propertyValue is decimal) { decimalProperty.Value.Value = (decimal)propertyValue; decimalProperty.Value.IsNull = false; } else { decimal decimalValue; if (Decimal.TryParse(propertyStringValue, out decimalValue)) { decimalProperty.Value.Value = decimalValue; decimalProperty.Value.IsNull = false; } } if (!decimalProperty.Value.IsNull) { properties.Add(decimalProperty); } } break; case SupportedTypes.CrmMoney: if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is decimal)) { var moneyProperty = new CrmMoneyProperty(); moneyProperty.Name = propertyName; moneyProperty.Value = new CrmMoney(); moneyProperty.Value.IsNull = true; if (propertyValue is decimal) { moneyProperty.Value.Value = (decimal)propertyValue; moneyProperty.Value.IsNull = false; } else { decimal moneyValue; if (Decimal.TryParse(propertyStringValue, out moneyValue)) { moneyProperty.Value.Value = moneyValue; moneyProperty.Value.IsNull = false; } } if (!moneyProperty.Value.IsNull) { properties.Add(moneyProperty); } } break; case SupportedTypes.CrmNumber: if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is int)) { var numberProperty = new CrmNumberProperty(); numberProperty.Name = propertyName; numberProperty.Value = new crm4.webservice.CrmNumber(); numberProperty.Value.IsNull = true; if (propertyValue is int) { numberProperty.Value.Value = (int)propertyValue; numberProperty.Value.IsNull = false; } else { int numberValue; if (Int32.TryParse(propertyStringValue, out numberValue)) { numberProperty.Value.Value = numberValue; numberProperty.Value.IsNull = false; } } if (!numberProperty.Value.IsNull) { properties.Add(numberProperty); } } break; case SupportedTypes.Picklist: PicklistContactAttribute picklistAttribute; if (this.CacheService.MetadataCache.ContainsKey(propertyName)) { picklistAttribute = (PicklistContactAttribute)this.CacheService.MetadataCache[propertyName]; } else { var attributeRequest = new RetrieveAttributeRequest(); attributeRequest.EntityLogicalName = EntityName.contact.ToString(); attributeRequest.LogicalName = propertyName; var attributeResponse = (RetrieveAttributeResponse)this.CrmMetadataService.Execute(attributeRequest); var attributeMetadata = (PicklistAttributeMetadata)attributeResponse.AttributeMetadata; var options = attributeMetadata.Options.ToDictionary(option => option.Label.UserLocLabel.Label, option => option.Value.Value); picklistAttribute = new PicklistContactAttribute(SupportedTypes.Picklist, options); this.CacheService.MetadataCache.Add(propertyName, picklistAttribute); } if (!String.IsNullOrEmpty(propertyStringValue)) { var value = picklistAttribute.Options[propertyStringValue]; if (value >= 0) { var picklistProperty = new PicklistProperty(); picklistProperty.Name = propertyName; picklistProperty.Value = new Picklist(); picklistProperty.Value.Value = value; properties.Add(picklistProperty); } else { CrmHelper.ShowMessage(String.Format("The picklist value ({0}) of the '{1}' property couldn't be recognized", propertyValue, propertyName)); break; } } break; } }
//Field Value Object /// <summary> /// Returns the entity attribute /// </summary> /// <param name="dynamicEntityInstance">Entity</param> /// <param name="entityPropertyLogicalName">Attribute name<</param> /// <returns>Attribute value(Object)</returns> public static Object GetPropertyObj(this DynamicEntity dynamicEntityInstance, string entityPropertyLogicalName) { if (dynamicEntityInstance.Properties.Contains(entityPropertyLogicalName)) { string propertyType = null; Object returnObject; try { //"CrmTypes" propertyType = dynamicEntityInstance.Properties[entityPropertyLogicalName].GetType().ToString(); } catch (Exception e) { propertyType = e.Message; returnObject = e.Message; return(null); } if (propertyType == "Microsoft.Crm.Sdk.Owner") { Owner ownerObject = (dynamicEntityInstance[entityPropertyLogicalName]) as Owner; returnObject = new { Value = ownerObject, Type = propertyType }; return(returnObject); } if (propertyType == "Microsoft.Crm.Sdk.Customer") { Customer customerObject = (dynamicEntityInstance[entityPropertyLogicalName]) as Customer; returnObject = new { Value = customerObject, Type = propertyType }; return(returnObject); } if (propertyType == "Microsoft.Crm.Sdk.CrmDateTime") { CrmDateTimeProperty datetimeObject = new CrmDateTimeProperty(entityPropertyLogicalName, (CrmDateTime)dynamicEntityInstance.Properties[entityPropertyLogicalName]); returnObject = new { Value = datetimeObject, Type = propertyType }; return(returnObject); } if (propertyType == "Microsoft.Crm.Sdk.CrmBoolean") { CrmBooleanProperty booleanObject = new CrmBooleanProperty(entityPropertyLogicalName, (CrmBoolean)dynamicEntityInstance.Properties[entityPropertyLogicalName]); returnObject = new { Value = booleanObject, Type = propertyType }; return(returnObject); } if (propertyType == "System.String") { returnObject = new { Value = (dynamicEntityInstance.Properties[entityPropertyLogicalName]), Type = propertyType }; return(returnObject); } if (propertyType == "Microsoft.Crm.Sdk.Lookup") { Lookup lookupObject = dynamicEntityInstance[entityPropertyLogicalName] as Lookup; returnObject = new { Value = lookupObject, Type = propertyType }; return(returnObject); } if (propertyType == "Microsoft.Crm.Sdk.Picklist") { Picklist piclkistObject = dynamicEntityInstance[entityPropertyLogicalName] as Picklist; returnObject = new { Value = piclkistObject, Type = propertyType }; return(returnObject); } } else { object nullObject = new { value = "Null", type = "Null" }; return(nullObject); } object nullreturn = new { value = "null", type = "null" }; return(nullreturn); }
/// <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"); } }
public Property CreateProperty(string name, CRMSecurityProvider.Sources.Attribute.CrmAttributeType type, string value, params string[] data) { Property property = null; switch (type) { case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Boolean: property = new CrmBooleanProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Customer: property = new CustomerProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.DateTime: property = new CrmDateTimeProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Decimal: property = new CrmDecimalProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Float: property = new CrmFloatProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Integer: property = new CrmNumberProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Lookup: property = new LookupProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Memo: property = new StringProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Money: property = new CrmMoneyProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Owner: property = new OwnerProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Picklist: property = new PicklistProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.State: property = new StateProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Status: property = new StatusProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.String: property = new StringProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.UniqueIdentifier: property = new UniqueIdentifierProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.PartyList: property = new DynamicEntityArrayProperty(); break; case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Virtual: case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.CalendarRules: case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.Internal: case CRMSecurityProvider.Sources.Attribute.CrmAttributeType.PrimaryKey: break; } if (property != null) { SetPropertyValue(property, value, data); } return(property); }
/// <summary> /// Creates the instance. /// </summary> /// <param name="propertyName">Name of the property.</param> /// <param name="value">The value.</param> /// <returns></returns> /// <exception cref="ArgumentException">value</exception> public virtual Property GetProperty(string propertyName, object value) { Property property; if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentException("Property name must be non null.", "propertyName"); } if (value == null) { throw new ArgumentNullException("Property value must be non null.", "value"); } if (string.Compare(propertyName, "StateCode", true, CultureInfo.InvariantCulture) == 0) { property = new StateProperty { Value = (string)value }; } else if (value is string) { property = new StringProperty { Value = (string)value }; } else if (value is UniqueIdentifier) { property = new UniqueIdentifierProperty { Value = (UniqueIdentifier)value }; } else if (value is Status) { property = new StatusProperty { Value = (Status)value }; } else if (value is Picklist) { property = new PicklistProperty { Value = (Picklist)value }; } else if (value is Owner) { property = new OwnerProperty { Value = (Owner)value }; } else if (value is Lookup) { property = new LookupProperty { Value = (Lookup)value }; } else if (value is CrmCampaignIntegration.Services.Key) { property = new KeyProperty { Value = (CrmCampaignIntegration.Services.Key)value }; } else if (value is EntityNameReference) { property = new EntityNameReferenceProperty { Value = (EntityNameReference)value }; } else if (value is DynamicEntity[]) { property = new DynamicEntityArrayProperty { Value = (DynamicEntity[])value }; } else if (value is Customer) { property = new CustomerProperty { Value = (Customer)value }; } else if (value is CrmCampaignIntegration.Services.CrmNumber) { property = new CrmNumberProperty { Value = (CrmCampaignIntegration.Services.CrmNumber)value }; } else if (value is CrmMoney) { property = new CrmMoneyProperty { Value = (CrmMoney)value }; } else if (value is CrmCampaignIntegration.Services.CrmFloat) { property = new CrmFloatProperty { Value = (CrmCampaignIntegration.Services.CrmFloat)value }; } else if (value is CrmDecimal) { property = new CrmDecimalProperty { Value = (CrmDecimal)value }; } else if (value is CrmCampaignIntegration.Services.CrmDateTime) { property = new CrmDateTimeProperty { Value = (CrmCampaignIntegration.Services.CrmDateTime)value }; } else if (value is CrmCampaignIntegration.Services.CrmBoolean) { property = new CrmBooleanProperty { Value = (CrmCampaignIntegration.Services.CrmBoolean)value }; } else { throw new NotSupportedException(string.Format("Unknown value type: {0}", value.GetType())); } property.Name = propertyName; return(property); }