public static string GetAliasedValue(CRM.Entity entity, string fieldName) { if (entity == null || string.IsNullOrEmpty(fieldName)) { return(string.Empty); } if (!entity.Contains(fieldName)) { return(string.Empty); } CRM.AliasedValue alias = (CRM.AliasedValue)entity[fieldName]; if (alias.Value.GetType().ToString() == "System.Guid" || alias.Value.GetType().ToString() == "System.String" || alias.Value.GetType().ToString() == "System.Boolean" || alias.Value.GetType().ToString() == "System.Int32") { return(alias.Value.ToString()); } if (alias.Value.GetType().ToString() == "System.Double") { return(Convert.ToDouble(alias.Value, CultureInfo.GetCultureInfo("en-us")).ToString("###0.000000", CultureInfo.GetCultureInfo("en-us")).Replace(',', '.')); } CRM.EntityReference referencia = (CRM.EntityReference)alias.Value; return(referencia.Name); }
public static Guid GetAliasedId(CRM.Entity entity, string fieldName) { if (entity == null || string.IsNullOrEmpty(fieldName)) { return(Guid.Empty); } if (!entity.Contains(fieldName)) { return(Guid.Empty); } CRM.AliasedValue alias = (CRM.AliasedValue)entity[fieldName]; switch (alias.Value.GetType().ToString()) { case "Microsoft.Xrm.Sdk.EntityReference": return(((CRM.EntityReference)alias.Value).Id); case "System.Guid": return(new Guid(alias.Value.ToString())); } return(Guid.Empty); }
/// <summary> /// Simplifies getting values from linked entities attributes wrapped in AliasedValue class /// This kind of attributes can be queried by FetchExpression or QueryExpression using Linked Entities /// </summary> /// <typeparam name="T">Attribute value type</typeparam> /// <param name="attributeLogicalName">Optional. Attribute logical name</param> /// <param name="alias">>Entity alias used in LinkedEntity definition</param> /// <returns>Attribute value</returns> public static T GetAliasedValue <T>(this Entity entity, String attributeLogicalName, String alias) { CheckParam.CheckForNull(alias, nameof(alias)); String aliasedAttributeName = attributeLogicalName != null? alias + "." + attributeLogicalName : alias; AliasedValue aliasedValue = entity.GetAttributeValue <AliasedValue>(aliasedAttributeName); if (aliasedValue != null) { return((T)aliasedValue.Value); } else { return(default);