Class XrmExtensions
Extensions methods relating to Xrm SDK.
public static class XrmExtensions
- Inheritance
-
XrmExtensions
- Inherited Members
Methods
AttributeValueEquals(object?, object?, bool)
Compares to attribute values for equality.
public static bool AttributeValueEquals(object? valueA, object? valueB, bool normalizeValues)
Parameters
valueA
objectThe first value
valueB
objectThe second value
normalizeValues
boolWhether to normalize string and DateTime values.
Returns
ContainsNonNullValue(Entity, string)
Whether the given attribute has a non-null value.
public static bool ContainsNonNullValue(this Entity target, string attribute)
Parameters
Returns
Copy(Entity)
Generates a deep copy of the target entity.
public static Entity Copy(this Entity target)
Parameters
target
Entity
Returns
CopyAndUpdateFrom(Entity, Entity)
Create a copy of baseEntity
and merges into the copy the
attributes from mergedEntity
, excluding attributes of type AliasedValue.
This is intended to be used to merge a newer version of an entity on top of an older one:
- preImage.CopyAndUpdateFrom(target)
These usages would likely produce undesired results:
- preImage.CopyAndUpdateFrom(postImage)
- target.CopyAndUpdateFrom(postImage)
- target.CopyAndUpdateFrom(preImage)
- postImage.CopyAndUpdateFrom(target)
- postImage.CopyAndUpdateFrom(preImage)
public static Entity CopyAndUpdateFrom(this Entity baseEntity, Entity mergedEntity)
Parameters
baseEntity
EntityThe entity image that is copied. Values from
mergedEntity
are merged into the copy.mergedEntity
EntityThe entity image whose attributes are merged down onto the copy of
baseEntity
.
Returns
CopyAttribute(Entity, string)
Creates a deep copy of the specified attributes on the target entity. Does not support copying AliasedValue attributes.
public static object? CopyAttribute(this Entity target, string attribute)
Parameters
Returns
GetFieldDisplayText(Entity, string)
Returns the formatted value, if present; otherwise, the actual value. Note that this typically only works for records retrieved from retrieve requests.
public static string GetFieldDisplayText(this Entity target, string attribute)
Parameters
Returns
GetFieldValue(Entity, string)
Returns the value of the specified field unwrapping any alias values.
public static object? GetFieldValue(this Entity target, string attribute)
Parameters
Returns
GetFieldValueAsString(Entity, string, bool)
Returns a string version of the field.
public static string GetFieldValueAsString(this Entity target, string attribute, bool useDisplayedValue = false)
Parameters
target
EntityThe target entity.
attribute
stringThe attribute to get the value for.
useDisplayedValue
boolIf false, the non-localized value will be returned. If true, a value for displaying to the user will be returned.
Returns
GetFieldValue<T>(Entity, string)
Returns the value of the field unwrapping AliasedValue/EntityReference/Money/OptionSetValue/OptionSetValueCollection if necessary.
public static T? GetFieldValue<T>(this Entity target, string attribute)
Parameters
Returns
- T
Type Parameters
T
The type to return.
GetFieldValue<T>(Entity, string, T?)
Returns the value of the field unwrapping AliasedValue/EntityReference/Money/OptionSetValue/OptionSetValueCollection if necessary.
public static T? GetFieldValue<T>(this Entity target, string attribute, T? defaultValue)
Parameters
target
EntityThe entity to get the value from
attribute
stringThe field to get the value for
defaultValue
TThe default value to use if the entity doesn't contain the given attribute or it is null.
Returns
- T
Type Parameters
T
The type to return.
GetItem<T>(ParameterCollection, string, T?)
Gets an item cast to the specified type from the ParameterCollection.
public static T? GetItem<T>(this ParameterCollection pc, string key, T? defaultValue = default)
Parameters
pc
ParameterCollectionThe ParameterCollection from which the item will be retrieved.
key
stringThe key to the item in the ParameterCollection.
defaultValue
TThe default value to be returned if the item cannot be cast, is null, or cannot be found.
Returns
- T
The value retrieved from the ParameterCollection cast as the specified type or the defaultValue if the item cannot be found or is null.
Type Parameters
T
The type as which the item will be returned.
IsAttributeEqual(Entity, Entity, string, bool)
Checks whether the specified attribute is the same in both entities. NOTE: only normalization possible without metadata is done.
public static bool IsAttributeEqual(this Entity target, Entity other, string attribute, bool normalizeValues = true)
Parameters
target
EntityTarget entity for checking
other
EntityEntity to compare to
attribute
stringThe attribute to compare
normalizeValues
boolWhether to normalize string and DateTime values.
Returns
ToEntitiesList<T>(EntityCollection)
Converts the EntityCollection to a list of entities with the given early bound type.
public static List<T> ToEntitiesList<T>(this EntityCollection collection) where T : Entity
Parameters
collection
EntityCollection
Returns
- List<T>
Type Parameters
T
The early bound entity type to use.
ToEntity(EntityReference)
Creates an entity referring to the given target intended to be passed to a Update or Upsert request.
public static Entity? ToEntity(this EntityReference target)
Parameters
target
EntityReference
Returns
ToEntityReference(Guid, string)
Transforms a Guid into an EntityReference.
public static EntityReference? ToEntityReference(this Guid target, string logicalName)
Parameters
target
GuidThe target Guid to transform into an EntityReference.
logicalName
stringThe logical name of the entity for which the Guid is a reference.
Returns
- EntityReference
An EntityReference with the specified Guid as the Id and logical entity name as the referenced entity, or a null value if the Guid is an empty Guid.
ToEntityReference(Guid?, string)
Transforms a Guid into an EntityReference.
public static EntityReference? ToEntityReference(this Guid? target, string logicalName)
Parameters
target
Guid?The target Guid to transform into an EntityReference.
logicalName
stringThe logical name of the entity for which the Guid is a reference.
Returns
- EntityReference
An EntityReference with the specified Guid as the Id and logical entity name as the referenced entity, or a null value if the Guid is null.
ToMoney(decimal)
Transforms the decimal value into a Money object.
public static Money ToMoney(this decimal target)
Parameters
target
decimalThe target decimal value to be transformed into a Money object.
Returns
- Money
A Money object whose value will be set to the value of the target decimal.
ToMoney(decimal?)
Transforms the decimal value into a Money object.
public static Money? ToMoney(this decimal? target)
Parameters
target
decimal?The target decimal value to be transformed into a Money object.
Returns
- Money
A Money object whose value will be set to the value of the target decimal, or null if the decimal value is null.
ToOptionSetValue(int)
Transforms the int value into a OptionSetValue object.
public static OptionSetValue ToOptionSetValue(this int target)
Parameters
target
intThe target int value to be transformed into an OptionSetValue object.
Returns
- OptionSetValue
An OptionSetValue object whose value will be set to the value of the target int.
ToOptionSetValue(int?)
Transforms the int value into an OptionSetValue object.
public static OptionSetValue? ToOptionSetValue(this int? target)
Parameters
target
int?The target int value to be transformed into an OptionSetValue object.
Returns
- OptionSetValue
A OptionSetValue object whose value will be set to the value of the target int, or null if the int value is null.
ToOptionSetValue<T>(T?)
Creates an option set value for the given enum value.
public static OptionSetValue? ToOptionSetValue<T>(this T? enumValue) where T : struct, Enum
Parameters
enumValue
T?
Returns
Type Parameters
T
ToOptionSetValue<T>(T)
Creates an option set value for the given enum value.
public static OptionSetValue ToOptionSetValue<T>(this T enumValue) where T : struct, Enum
Parameters
enumValue
T
Returns
Type Parameters
T
TryRemoveUnchangedFields(Entity, Entity)
Removes unchanged fields where it is possible to identify the values are the same without querying metadata. Doesn't deal with the items below (because it would require metadata or an additional query to CDS):
- In order to be eligible to be removed, DateTime.Kind in updateEntity should match what CRM returns from retrieves (Unspecified for Time Zone Independent and Date Only and Utc for user local)
- Zeroing hours/minutes/seconds for a date only field. Does handle zeroing milliseconds and below since CDS only handles second-level resolution.
- Number/Money precision
- Alt Key References vs regular EntityReference on currentEntity
public static void TryRemoveUnchangedFields(this Entity updateEntity, Entity currentEntity)