private static string MapAttributesToJsonTemplate(Microsoft.Xrm.Sdk.Entity record, string jsonTemplate, string orgUrl, bool doHyperlinks = true, bool doLinksAsAdaptive = true) { try { if (!string.IsNullOrEmpty(jsonTemplate)) { foreach (KeyValuePair <string, object> kvp in record.Attributes) { if (!jsonTemplate.Contains("{" + kvp.Key + "}")) { // move to next element if not detected within the template continue; } string type = kvp.Value.GetType().Name.ToLower(); string value = kvp.Value.ToString(); string hyperlink = string.Empty; switch (type) { case ("entityreference"): { Microsoft.Xrm.Sdk.EntityReference refVal = (Microsoft.Xrm.Sdk.EntityReference)kvp.Value; value = refVal.Name; if (doHyperlinks) { hyperlink = orgUrl + "/main.aspx?forceUCI=1&etn=" + refVal.LogicalName + "&id=" + refVal.Id + "&pagetype=entityrecord"; } break; } case ("optionsetvalue"): { value = record.GetValueDisplayString(kvp.Key); break; } case ("datetime"): { DateTime dtVal = (DateTime)kvp.Value; value = dtVal.ToString("dd/MM/yyyy"); break; } case ("money"): { Money mVal = (Money)kvp.Value; value = mVal.Value.ToString("#,###,##0"); break; } case ("entitycollection"): { if (kvp.Value != null) { Microsoft.Xrm.Sdk.EntityCollection collection = (Microsoft.Xrm.Sdk.EntityCollection)kvp.Value; value = collection.ToCommaSeparatedString(); } else { value = "None"; } break; } default: { object o = kvp.Value; if (o != null) { value = o.ToString(); } else { value = ""; } break; } } if (!String.IsNullOrEmpty(hyperlink)) { if (doLinksAsAdaptive) { // In AdaptiveCards, Links are formatted as [caption](url link) jsonTemplate = jsonTemplate.Replace("{" + kvp.Key.ToLower() + "}", "[" + value + "](" + hyperlink + ")"); } else { jsonTemplate = jsonTemplate.Replace("{" + kvp.Key.ToLower() + "}", hyperlink); } } else { jsonTemplate = jsonTemplate.Replace("{" + kvp.Key.ToLower() + "}", value); } if (orgUrl != null) { jsonTemplate = jsonTemplate.Replace("{recordurl}", orgUrl + "/main.aspx?" + "etn=" + record.LogicalName + "&id=%7B" + record.Id + "%7D" + "&pagetype=entityrecord" + "&forceUCI=1"); } } // final step - remove all occuring strings by "{ and }" - effectively blanking the unmapped schemas string result = jsonTemplate; while (true) { int indexOfOpen = result.IndexOf("\"{"); int indexOfClose = result.IndexOf("}\"", indexOfOpen + 1); if (indexOfOpen < 0 && indexOfClose < 0) { break; } result = result.Substring(0, indexOfOpen) + "\"" + result.Substring(indexOfClose + 1); } if (result != jsonTemplate) { jsonTemplate = result; } } return(jsonTemplate); } catch (Exception e) { throw new Exception("SS21.Examples.TeamBot.AdaptiveCardFactory.MapAttributesToJsonTemplate :: ERROR :: Unable to map attributes to JSON template due to the following reason/s " + Environment.NewLine + e.Message); } }