示例#1
0
 public string GetFieldAsDisplayString(string recordType, string fieldName, object value, LocalisationService localisationService, bool isHtml = false, string func = null)
 {
     if (value == null)
     {
         return("");
     }
     else if (value is string)
     {
         if (isHtml)
         {
             return(((string)value).Replace(Environment.NewLine, "<br />").Replace("\n", "<br />"));
         }
         else
         {
             return((string)value);
         }
     }
     else if (value is EntityReference)
     {
         return(XrmEntity.GetLookupName(value));
     }
     else if (value is OptionSetValue)
     {
         if (value is OptionSetValue)
         {
             return(GetOptionLabel(((OptionSetValue)value).Value, fieldName, recordType));
         }
         throw new Exception("Value Type Not Matched For OptionSetValue " + value.GetType().Name);
     }
     else if (value is Money)
     {
         return(XrmEntity.GetMoneyValue(value).ToString("$##,###,###,###,##0.00"));
     }
     else if (value is DateTime)
     {
         var dt = (DateTime)value;
         if (dt.Kind == DateTimeKind.Utc)
         {
             dt = localisationService.ConvertToTargetTime(dt);
         }
         if (func == "year")
         {
             return(dt.ToString("yyyy"));
         }
         if (GetDateFormat(fieldName, recordType) == DateTimeFormat.DateAndTime)
         {
             return(dt.ToString("dd/MM/yyyy hh:mm:ss tt"));
         }
         return(dt.Date.ToString("dd/MM/yyyy"));
     }
     else if (IsActivityParty(fieldName, recordType))
     {
         if (value is Entity[])
         {
             var namesToOutput = new List <string>();
             foreach (var party in (Entity[])value)
             {
                 namesToOutput.Add(XrmEntity.GetLookupName(party, "partyid"));
             }
             return(string.Join(", ", namesToOutput.Where(f => !string.IsNullOrWhiteSpace(f))));
         }
     }
     else if (value is bool)
     {
         var metadata = GetFieldMetadata(fieldName, recordType) as BooleanAttributeMetadata;
         if (metadata != null)
         {
             var boolValue = (bool)value;
             if (boolValue && metadata.OptionSet != null && metadata.OptionSet.TrueOption != null && metadata.OptionSet.TrueOption.Label != null)
             {
                 return(GetLabelDisplay(metadata.OptionSet.TrueOption.Label));
             }
             if (!boolValue && metadata.OptionSet != null && metadata.OptionSet.FalseOption != null && metadata.OptionSet.FalseOption.Label != null)
             {
                 return(GetLabelDisplay(metadata.OptionSet.FalseOption.Label));
             }
             return(value.ToString());
         }
     }
     return(value.ToString());
 }