-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEntityExtensions.cs
756 lines (701 loc) · 29.9 KB
/
EntityExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using Rappen.XRM.Helpers.Interfaces;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Rappen.XRM.Helpers.Extensions
{
/// <summary>
/// Light-weight features inspired by CintDynEntity
/// </summary>
public static class EntityExtensions
{
private static string guidtemplate = "FFFFEEEEDDDDCCCCBBBBAAAA99998888";
private static Guid StringToGuidish(string strId, ILogger log)
{
Guid id = Guid.Empty;
if (!string.IsNullOrWhiteSpace(strId) &&
!Guid.TryParse(strId, out id))
{
log.StartSection("StringToGuidish");
log.Log($"String: {strId}");
string template = guidtemplate;
if (Guid.TryParse(template.Substring(0, 32 - strId.Length) + strId, out id))
{
log.Log($"Composed temporary guid from template + incomplete id: {id}");
}
else
{
log.Log($"Failed to compose temporary guid from: {strId}");
}
log.EndSection();
}
return id;
}
private static bool StringToBool(string value)
{
if (value == "0")
{
return false;
}
else if (value == "1")
{
return true;
}
else
{
return bool.Parse(value);
}
}
#region Public Methods
public static object AttributeToBaseType(object attribute)
{
if (attribute is AliasedValue aliasedvalue)
{
return AttributeToBaseType(aliasedvalue.Value);
}
else if (attribute is EntityReference entref)
{
if (!string.IsNullOrEmpty(entref.LogicalName) && !entref.Id.Equals(Guid.Empty))
{
return entref.Id;
}
return null;
}
else if (attribute is OptionSetValue osv)
{
return osv.Value;
}
else if (attribute is OptionSetValueCollection copt)
{
return copt.Select(c => c.Value);
}
else if (attribute is Money money)
{
return money.Value;
}
else
{
return attribute;
}
}
/// <summary>
/// Generic method to add property with "name" and set its value of type "T" to "value"
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <param name="name"></param>
/// <param name="value"></param>
public static void AddProperty<T>(this Entity entity, string name, T value)
{
if (entity.Attributes.Contains(name))
{
entity.Attributes[name] = value;
}
else
{
entity.Attributes.Add(name, value);
}
}
internal static void AddProperty(this Entity entity, string attribute, string type, string value, ILogger log)
{
log.StartSection("AddProperty");
log.Log($"{attribute} = \"{value}\" ({type})");
switch (type)
{
case "String":
case "Memo":
entity.AddProperty(attribute, value);
break;
case "Int32":
case "Integer":
if (!string.IsNullOrWhiteSpace(value))
{
entity.AddProperty(attribute, Int32.Parse(value));
}
break;
case "Int64":
if (!string.IsNullOrWhiteSpace(value))
{
entity.AddProperty(attribute, Int64.Parse(value));
}
break;
case "OptionSetValue":
case "Picklist":
case "State":
case "Status":
if (!string.IsNullOrWhiteSpace(value))
{
entity.AddProperty(attribute, new OptionSetValue(int.Parse(value)));
}
break;
case "EntityReference":
case "Lookup":
case "Customer":
case "Owner":
if (!string.IsNullOrWhiteSpace(value))
{
var valueparts = value.Split(':');
string entityname = valueparts[0];
value = valueparts[1];
Guid refId = StringToGuidish(value, log);
var entref = new EntityReference(entityname, refId);
if (valueparts.Length > 2)
{
entref.Name = valueparts[2];
}
entity.AddProperty(attribute, entref);
}
break;
case "DateTime":
if (!string.IsNullOrWhiteSpace(value))
{
entity.AddProperty(attribute, DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal));
}
break;
case "Boolean":
if (!string.IsNullOrWhiteSpace(value))
{
entity.AddProperty(attribute, StringToBool(value));
}
break;
case "Guid":
case "Uniqueidentifier":
if (!string.IsNullOrWhiteSpace(value))
{
Guid uId = StringToGuidish(value, log);
entity.AddProperty(attribute, uId);
}
break;
case "Decimal":
if (!string.IsNullOrWhiteSpace(value))
{
entity.AddProperty(attribute, decimal.Parse(value));
}
break;
case "Money":
if (!string.IsNullOrWhiteSpace(value))
{
entity.AddProperty(attribute, new Money(decimal.Parse(value)));
}
break;
case "null":
entity.Attributes.Add(attribute, null);
break;
default:
throw new ArgumentOutOfRangeException("Type", type, "Cannot parse attibute type");
}
log.EndSection();
}
/// <summary>
///
/// </summary>
/// <param name="entity"></param>
/// <param name="bag"></param>
/// <param name="onlyId"></param>
/// <returns></returns>
public static Entity Clone(this Entity entity, bool onlyId, IBag bag)
{
if (entity == null)
{
return null;
}
var clone = new Entity(entity.LogicalName, entity.Id);
if (!onlyId)
{
// Preparing all attributes except the one in which entity id is stored
var attributes = entity.Attributes.Where(x => x.Key.ToLowerInvariant() != $"{clone.LogicalName}id".ToLowerInvariant() || (Guid)x.Value != clone.Id);
clone.Attributes.AddRange(attributes.Where(a => !clone.Attributes.Contains(a.Key)));
}
bag?.Logger.Log($"Cloned {entity.LogicalName} {entity.Id} with {entity.Attributes.Count} attributes");
return clone;
}
/// <summary>
///
/// </summary>
/// <param name="entity"></param>
/// <param name="name"></param>
/// <param name="notnull"></param>
/// <returns></returns>
public static bool Contains(this Entity entity, string name, bool notnull) => entity.Attributes.Contains(name) && (!notnull || entity.Attributes[name] != null);
public static T GetAttributeValue<T>(this Entity entity, string attribute, T def)
{
if (entity.Contains(attribute) && entity[attribute] is T result)
{
return result;
}
return def;
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="bag"></param>
/// <param name="related"></param>
/// <param name="columns"></param>
/// <returns></returns>
public static Entity GetRelated(this Entity source, IBag bag, string related, params string[] columns) => source.GetRelated(bag, related, new ColumnSet(columns));
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="bag"></param>
/// <param name="related"></param>
/// <param name="columns"></param>
/// <returns></returns>
public static Entity GetRelated(this Entity source, IBag bag, string related, ColumnSet columns)
{
bag.Logger.StartSection($"GetRelated {related} from {source.LogicalName} {source.Id}");
Entity result = null;
var refname = related;
var refatt = string.Empty;
if (refname.Contains("."))
{
refatt = refname.Substring(refname.IndexOf('.') + 1);
refname = refname.Substring(0, refname.IndexOf('.'));
}
if (source.Attributes.Contains(refname))
{
EntityReference reference = null;
if (source.Attributes[refname] is EntityReference er)
{
reference = er;
}
else if (source.Attributes[refname] is Guid id && refname.EndsWith("id"))
{
reference = new EntityReference(string.Empty, id);
}
if (string.IsNullOrEmpty(reference.LogicalName))
{
//reference.LogicalName = Common.CintDynEntity.GetRelatedEntityNameFromLookupAttributeName(refname);
}
if (reference != null)
{
if (refatt != "")
{
var nextref = refatt;
if (nextref.Contains("."))
{
nextref = nextref.Substring(0, nextref.IndexOf('.'));
}
bag.Logger.Log($"Loading {reference.LogicalName} {reference.Id} column {nextref}");
var cdNextRelated = bag.Service.Retrieve(reference.LogicalName, reference.Id, new ColumnSet(new string[] { nextref }));
if (cdNextRelated != null)
{
result = cdNextRelated.GetRelated(bag, refatt, columns);
}
}
else
{
result = bag.Service.Retrieve(reference.LogicalName, reference.Id, columns);
}
}
}
else
{
bag.Logger.Log($"Record does not contain attribute {refname}");
}
if (result == null)
{
bag.Logger.Log("Could not load related record");
}
else
{
bag.Logger.Log($"Loaded related {result.LogicalName} {result.Id}");
}
bag.Logger.EndSection();
return result;
}
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="bag"></param>
/// <param name="entity"></param>
/// <param name="referencingattribute"></param>
/// <param name="onlyactive"></param>
/// <param name="columns"></param>
/// <returns></returns>
public static EntityCollection GetRelating(this Entity source, IBag bag, string entity, string referencingattribute, bool onlyactive, params string[] columns) => source.GetRelating(bag, entity, referencingattribute, onlyactive, null, null, new ColumnSet(columns), false);
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="bag"></param>
/// <param name="entity"></param>
/// <param name="referencingattribute"></param>
/// <param name="onlyactive"></param>
/// <param name="extrafilter"></param>
/// <param name="orders"></param>
/// <param name="columns"></param>
/// <param name="nolock"></param>
/// <returns></returns>
public static EntityCollection GetRelating(this Entity source, IBag bag, string entity, string referencingattribute, bool onlyactive, FilterExpression extrafilter, OrderExpression[] orders, ColumnSet columns, bool nolock)
{
bag.Logger.StartSection($"GetRelating {entity} where {referencingattribute}={source.Id} and active={onlyactive}");
var qry = new QueryExpression(entity);
qry.Criteria.AddCondition(referencingattribute, ConditionOperator.Equal, source.Id);
if (onlyactive)
{
qry.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
}
qry.ColumnSet = columns;
if (extrafilter != null)
{
bag.Logger.Log($"Adding filter with {extrafilter.Conditions.Count} conditions");
qry.Criteria.AddFilter(extrafilter);
}
if (orders != null && orders.Length > 0)
{
bag.Logger.Log($"Adding orders ({orders.Length})");
qry.Orders.AddRange(orders);
}
qry.NoLock = nolock;
var result = bag.Service.RetrieveMultiple(qry);
bag.Logger.Log($"Got {result.Entities.Count} records");
bag.Logger.EndSection();
return result;
}
/// <summary>
///
/// </summary>
/// <param name="entity1"></param>
/// <param name="bag"></param>
/// <param name="entity2"></param>
/// <returns></returns>
public static Entity Merge(this Entity entity1, Entity entity2, IBag bag = null)
{
if (bag != null)
{
bag.Logger.StartSection($"Merge {entity1?.LogicalName} {entity1?.ToStringExt(bag.Service)} with {entity2?.LogicalName} {entity2?.ToStringExt(bag.Service)}");
}
var merge = entity1.Clone(false, bag) ?? entity2.Clone(false, bag);
if (entity1 != null && entity2 != null)
{
merge.Attributes.AddRange(entity2.Attributes.Where(a => !merge.Attributes.Contains(a.Key)));
}
if (bag != null)
{
bag.Logger.Log($"Base entity had {entity1?.Attributes?.Count} attributes. Second entity {entity2?.Attributes?.Count}. Merged entity has {merge.Attributes.Count}");
bag.Logger.EndSection();
}
return merge;
}
/// <summary>
/// Generic method to retrieve property with name "name" of type "T"
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <param name="attributename"></param>
/// <param name="defaultvalue"></param>
/// <returns></returns>
public static T Property<T>(this Entity entity, string attributename, T defaultvalue) => (T)(object)(entity.Contains(attributename) && entity[attributename] is T ? (T)entity[attributename] : defaultvalue);
/// <summary>Gets the value of a property derived to its base type</summary>
/// <param name="entity"></param>
/// <param name="name"></param>
/// <param name="def"></param>
/// <param name="supresserrors"></param>
/// <returns>Base type of attribute</returns>
/// <remarks>Translates <c>AliasedValue, EntityReference, OptionSetValue and Money</c> to their underlying base types</remarks>
public static object PropertyAsBaseType(this Entity entity, string name, object def, bool supresserrors)
{
if (!entity.Contains(name))
{
if (!supresserrors)
{
throw new InvalidPluginExecutionException(string.Format("Attribute {0} not found in entity {1} {2}", name, entity.LogicalName, entity.Id));
}
else
{
return def;
}
}
return AttributeToBaseType(entity[name]);
}
/// <summary>Gets the value of a property as a formatted string</summary>
/// <param name="entity"></param>
/// <param name="bag"></param>
/// <param name="name"></param>
/// <param name="def"></param>
/// <param name="supresserrors"></param>
/// <param name="format"></param>
/// <returns></returns>
public static string PropertyAsString(this Entity entity, IBag bag, string name, string def = null, bool supresserrors = false, string format = null)
{
bool hasValueFormat = false;
if (!entity.Contains(name))
{
if (!supresserrors)
{
throw new InvalidPluginExecutionException(string.Format("Attribute {0} not found in entity {1} {2}", name, entity.LogicalName, entity.Id));
}
else
{
return def;
}
}
// Extrahera eventuella egna implementerade formatsträngar, t.ex. "<MaxLen=20>"
var extraFormats = new List<string>();
format = format.ExtractExtraFormatTags(extraFormats);
string result = null;
object oAttrValue = entity.Contains(name) ? entity[name] : null;
if (oAttrValue != null && format?.StartsWith("<value>") == true)
{
hasValueFormat = true;
format = format.Replace("<value>", "");
oAttrValue = AttributeToBaseType(oAttrValue);
}
if (oAttrValue != null && !string.IsNullOrWhiteSpace(format))
{
if (oAttrValue is AliasedValue)
{
oAttrValue = AttributeToBaseType(((AliasedValue)oAttrValue).Value);
}
if (format == "<entity>")
{
if (oAttrValue is EntityReference entref)
{
result = entref.LogicalName;
}
else if (oAttrValue is Guid guid)
{
result = entity.LogicalName;
}
}
else if (format == "<recordurl>")
{
if (oAttrValue is EntityReference entref)
{
result = bag.Service.GetEntityFormUrl(entref);
}
else if (oAttrValue is Guid guid)
{
result = bag.Service.GetEntityFormUrl(new EntityReference(entity.LogicalName, guid));
}
}
else if (oAttrValue is Money)
{
decimal dAttrValue = ((Money)oAttrValue).Value;
result = dAttrValue.ToString(format);
}
else if (oAttrValue is int)
{
result = ((int)oAttrValue).ToString(format);
}
else if (oAttrValue is decimal)
{
result = ((decimal)oAttrValue).ToString(format);
}
}
if (result == null)
{
if (oAttrValue != null && oAttrValue is EntityReference er)
{ // Introducerat för att nyttja metadata- och entitetscache på CrmServiceProxy
var related = entity.GetRelated(bag, name, bag.Service.GetPrimaryAttribute(er.LogicalName).LogicalName);
result = Utils.EntityToString(related, bag.Service);
}
else if (hasValueFormat)
{
if (oAttrValue is IEnumerable<int> manyint)
{
result = string.Join(";", manyint.Select(m => m.ToString()));
}
else
{
result = oAttrValue.ToString();
}
}
else
{
result = entity.AttributeToString(name, def, bag.Service);
}
if (!string.IsNullOrWhiteSpace(format))
{
DateTime tmpDateTime;
int tmpInt;
decimal tmpDecimal;
if (DateTime.TryParse(result, out tmpDateTime))
{
result = tmpDateTime.ToString(format);
}
else if (int.TryParse(result, out tmpInt))
{
result = tmpInt.ToString(format);
}
else if (decimal.TryParse(result.Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator).Replace(",", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), out tmpDecimal))
{
result = tmpDecimal.ToString(format);
}
else
{
if (!format.Contains("{0}"))
{
format = "{0:" + format + "}";
}
result = string.Format(format, result);
}
}
}
// Applicera eventuella egna implementerade formatsträngar
foreach (var extraFormat in extraFormats)
{
result = result.FormatByTag(extraFormat);
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="entity"></param>
/// <param name="name"></param>
public static void RemoveProperty(this Entity entity, string name)
{
if (entity.Contains(name))
{
entity.Attributes.Remove(name);
}
}
/// <summary>
/// Returns the name (value of Primary Attribute) of the entity. If PA is not available, the Guid is returned.
/// </summary>
/// <param name="entity"></param>
/// <returns>Primary Attribute value, or Primary Key</returns>
public static string ToStringExt(this Entity entity, IOrganizationService service) => Utils.EntityToString(entity, service);
/// <summary>Gets a readable string representation of given attribute</summary>
/// <param name="entity">Entity containing the attribute</param>
/// <param name="attributename">Name of the attribute</param>
/// <returns>String value of the attribute. If the attribute is nussing, null is returned.</returns>
public static string AttributeToString(this Entity entity, string attributename) => AttributeToString(entity, attributename, null, null);
/// <summary>Gets a readable string representation of given attribute</summary>
/// <param name="entity">Entity containing the attribute</param>
/// <param name="attributename">Name of the attribute</param>
/// <param name="service">Service to use for optionset/entityreference value retrieval</param>
/// <returns>String value of the attribute. If the attribute is nussing, null is returned.</returns>
public static string AttributeToString(this Entity entity, string attributename, IOrganizationService service) => AttributeToString(entity, attributename, null, service);
/// <summary>Gets a readable string representation of given attribute</summary>
/// <param name="entity">Entity containing the attribute</param>
/// <param name="attributename">Name of the attribute</param>
/// <param name="def">Default value if attribute is missing</param>
/// <returns>String value of the attribute. If the attribute is nussing, default is returned.</returns>
public static string AttributeToString(this Entity entity, string attributename, string def) => AttributeToString(entity, attributename, def, null);
/// <summary>Gets a readable string representation of given attribute</summary>
/// <param name="entity">Entity containing the attribute</param>
/// <param name="attributename">Name of the attribute</param>
/// <param name="def">Default value if attribute is missing</param>
/// <param name="service">Service to use for optionset/entityreference value retrieval</param>
/// <returns>String value of the attribute. If the attribute is nussing, default is returned.</returns>
public static string AttributeToString(this Entity entity, string attributename, string def, IOrganizationService service)
{
if (!string.IsNullOrWhiteSpace(attributename) && entity.Contains(attributename))
{
if (entity.FormattedValues.Contains(attributename) && !string.IsNullOrEmpty(entity.FormattedValues[attributename]))
{
return entity.FormattedValues[attributename];
}
else
{
return AttributeToString(entity, entity[attributename], attributename, service);
}
}
else
{
return def;
}
}
private static string AttributeToString(Entity entity, object attribute, string attributename, IOrganizationService service)
{
if (attribute is AliasedValue av)
{
return AttributeToString(entity, av.Value, av.AttributeLogicalName, service);
}
if (attribute is EntityReference refatt)
{
if (!string.IsNullOrEmpty(refatt.Name))
{
return refatt.Name;
}
if (service != null)
{
var primaryattribute = service.GetPrimaryAttribute(refatt.LogicalName).LogicalName;
var referencedentity = service.Retrieve(refatt.LogicalName, refatt.Id, new ColumnSet(primaryattribute));
if (referencedentity.Contains(primaryattribute))
{
return AttributeToString(referencedentity, primaryattribute, service);
}
}
return refatt.LogicalName + ":" + refatt.Id.ToString();
}
if (attribute is EntityCollection ecatt && ecatt.EntityName == "activityparty")
{
var result = new StringBuilder();
if (ecatt.Entities.Count > 0)
{
var partyAdded = false;
foreach (var activityparty in ecatt.Entities)
{
var party = "";
if (activityparty.Contains("partyid") && activityparty["partyid"] is EntityReference entref)
{
party = entref.Name;
}
if (string.IsNullOrEmpty(party) && activityparty.Contains("addressused"))
{
party = activityparty["addressused"].ToString();
}
if (string.IsNullOrEmpty(party))
{
party = activityparty.Id.ToString();
}
if (partyAdded)
{
result.Append(", ");
}
result.Append(party);
partyAdded = true;
}
}
return result.ToString();
}
if (attribute is OptionSetValueCollection optmultiatt)
{
if (service != null && service.GetAttribute(entity.LogicalName, attributename) is MultiSelectPicklistAttributeMetadata msam)
{
return string.Join(", ", optmultiatt.Select(o => msam?.OptionSet?.Options.FirstOrDefault(oo => oo.Value == o.Value)?.Label?.UserLocalizedLabel?.Label ?? o.Value.ToString()));
}
return string.Join("; ", optmultiatt.Select(o => o.Value.ToString()));
}
if (attribute is OptionSetValue optatt)
{
if (service != null && service.GetAttribute(entity.LogicalName, attributename) is EnumAttributeMetadata eam)
{
foreach (var oMD in eam?.OptionSet?.Options)
{
if (oMD.Value == optatt.Value)
{
return oMD.Label?.UserLocalizedLabel?.Label ?? oMD.Label.LocalizedLabels[0]?.Label;
}
}
}
return optatt.Value.ToString();
}
if (attribute is DateTime dt)
{
return dt.ToString("G");
}
if (attribute is Money money)
{
return money.Value.ToString("C");
}
if (attribute != null)
{
return attribute.ToString();
}
return null;
}
#endregion Public Methods
}
}