Example #1
0
        /// <summary>
        /// Parses an attribute array into a object that can be used to create a JSON request.
        /// </summary>
        /// <param name="entityAttributes"></param>
        /// <param name="mUtil"></param>
        /// <returns></returns>
        internal static ExpandoObject ToExpandoObject(AttributeCollection entityAttributes, MetadataUtility mUtil)
        {
            dynamic expando       = new ExpandoObject();
            var     expandoObject = ((IDictionary <string, object>)(expando));
            var     attributes    = entityAttributes.ToArray();

            foreach (var attrib in entityAttributes)
            {
                var keyValuePair = attrib;
                var value        = keyValuePair.Value;
                var key          = keyValuePair.Key;
                if (value is EntityReference entityReference)
                {
                    key   = $"{key}@odata.bind";
                    value = $"/{mUtil.GetEntityMetadata(Xrm.Sdk.Metadata.EntityFilters.Entity, entityReference.LogicalName).EntitySetName}({entityReference.Id})";
                }
                else
                {
                    key = key.ToLower();
                    if (value is OptionSetValueCollection optionSetValues)
                    {
                        string mselectValueString = string.Empty;
                        foreach (var opt in optionSetValues)
                        {
                            mselectValueString += $"{opt.Value.ToString()},";
                        }
                        value = mselectValueString.Remove(mselectValueString.Length - 1);
                    }
                    else if (value is OptionSetValue optionSetValue)
                    {
                        value = optionSetValue.Value.ToString();
                    }
                    else if (value is DateTime dateTimeValue)
                    {
                        value = dateTimeValue.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
                    }
                    else if (value is Money moneyValue)
                    {
                        value = moneyValue.Value;
                    }
                    else if (value is bool boolValue)
                    {
                        value = boolValue.ToString();
                    }
                    else if (value is Guid guidValue)
                    {
                        value = guidValue.ToString();
                    }
                    else if (value is null)
                    {
                        value = null;
                    }
                }
                expandoObject.Add(key, value);
            }
            return((ExpandoObject)(expandoObject));
        }
Example #2
0
        /// <summary>
        /// Parses an attribute array into a object that can be used to create a JSON request.
        /// </summary>
        /// <param name="entityAttributes"></param>
        /// <param name="mUtil"></param>
        /// <param name="entityName"></param>
        /// <returns></returns>
        internal static ExpandoObject ToExpandoObject(string entityName, AttributeCollection entityAttributes, MetadataUtility mUtil)
        {
            dynamic expando       = new ExpandoObject();
            var     expandoObject = (IDictionary <string, object>)expando;
            var     attributes    = entityAttributes.ToArray();

            // this is used to support ActivityParties collections
            List <ExpandoObject> partiesCollection = null;

            foreach (var attrib in entityAttributes)
            {
                var keyValuePair = attrib;
                var value        = keyValuePair.Value;
                var key          = keyValuePair.Key;
                if (value is EntityReference entityReference)
                {
                    // Get Lookup attribute meta data for the ER to check for polymorphic relationship.
                    var attributeInfo = mUtil.GetAttributeMetadata(entityName, key.ToLower());
                    if (attributeInfo is Xrm.Sdk.Metadata.LookupAttributeMetadata attribData)
                    {
                        // Now get relationship to make sure we use the correct name.
                        var eData     = mUtil.GetEntityMetadata(Xrm.Sdk.Metadata.EntityFilters.Relationships, entityName);
                        var ERNavName = eData.ManyToOneRelationships.FirstOrDefault(w => w.ReferencingAttribute.Equals(attribData.LogicalName) &&
                                                                                    w.ReferencedEntity.Equals(entityReference.LogicalName))
                                        ?.ReferencingEntityNavigationPropertyName;
                        if (!string.IsNullOrEmpty(ERNavName))
                        {
                            key = ERNavName;
                        }

                        // Populate Key property
                        key = $"{key}@odata.bind";
                    }
                    else if (attributeInfo == null)
                    {
                        // Fault here.
                        throw new CdsClientOperationException($"Entity Reference {key.ToLower()} was not found for entity {entityName}.", null);
                    }

                    string entityReferanceValue = string.Empty;
                    // process ER Value
                    if (entityReference.KeyAttributes?.Any() == true)
                    {
                        entityReferanceValue = ParseAltKeyCollection(entityReference.KeyAttributes);
                    }
                    else
                    {
                        entityReferanceValue = entityReference.Id.ToString();
                    }


                    value = $"/{mUtil.GetEntityMetadata(Xrm.Sdk.Metadata.EntityFilters.Entity, entityReference.LogicalName).EntitySetName}({entityReferanceValue})";
                }
                else
                {
                    if (value is EntityCollection)
                    {
                        // try to get the participation type id from the key.
                        int  PartyTypeId     = PartyListHelper.GetParticipationtypeMasks(key);
                        bool isActivityParty = PartyTypeId != -1;  // if the partytypeID is -1 this is not a activity party collection.

                        if (isActivityParty && partiesCollection == null)
                        {
                            partiesCollection = new List <ExpandoObject>(); // Only build it when needed.
                        }
                        // build linked collection here.
                        foreach (var ent in (value as EntityCollection).Entities)
                        {
                            ExpandoObject rslt = ToExpandoObject(ent.LogicalName, ent.Attributes, mUtil);
                            if (isActivityParty)
                            {
                                var tempDict = ((IDictionary <string, object>)rslt);
                                tempDict.Add("participationtypemask", PartyTypeId);
                                partiesCollection.Add((ExpandoObject)tempDict);
                            }
                        }
                        if (isActivityParty)
                        {
                            continue;
                        }

                        // Note.. if this is not an activity party but instead an embedded entity.. this will fall though and fail with trying to embed an entity.
                    }
                    else
                    {
                        key = key.ToLower();
                        if (value is OptionSetValueCollection optionSetValues)
                        {
                            string mselectValueString = string.Empty;
                            foreach (var opt in optionSetValues)
                            {
                                mselectValueString += $"{opt.Value.ToString()},";
                            }
                            value = mselectValueString.Remove(mselectValueString.Length - 1);
                        }
                        else if (value is OptionSetValue optionSetValue)
                        {
                            value = optionSetValue.Value.ToString();
                        }
                        else if (value is DateTime dateTimeValue)
                        {
                            var attributeInfo = mUtil.GetAttributeMetadata(entityName, key.ToLower());
                            if (attributeInfo is Xrm.Sdk.Metadata.DateTimeAttributeMetadata attribDateTimeData)
                            {
                                if (attribDateTimeData.Format == Xrm.Sdk.Metadata.DateTimeFormat.DateOnly)
                                {
                                    value = dateTimeValue.ToUniversalTime().ToString("yyyy'-'MM'-'dd'");
                                }
                                else
                                {
                                    value = dateTimeValue.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
                                }
                            }
                        }
                        else if (value is Money moneyValue)
                        {
                            value = moneyValue.Value;
                        }
                        else if (value is bool boolValue)
                        {
                            value = boolValue.ToString();
                        }
                        else if (value is Guid guidValue)
                        {
                            value = guidValue.ToString();
                        }
                        else if (value is null)
                        {
                            value = null;
                        }
                    }
                }
                expandoObject.Add(key, value);
            }

            // Check to see if this contained an activity party
            if (partiesCollection?.Count > 0)
            {
                expandoObject.Add($"{entityName}_activity_parties", partiesCollection);
            }


            return((ExpandoObject)expandoObject);
        }
        /// <summary>
        /// Parses an attribute array into a object that can be used to create a JSON request.
        /// </summary>
        /// <param name="entityAttributes"></param>
        /// <param name="mUtil"></param>
        /// <param name="entityName"></param>
        /// <returns></returns>
        internal static ExpandoObject ToExpandoObject(string entityName, AttributeCollection entityAttributes, MetadataUtility mUtil)
        {
            dynamic expando       = new ExpandoObject();
            var     expandoObject = ((IDictionary <string, object>)(expando));
            var     attributes    = entityAttributes.ToArray();

            foreach (var attrib in entityAttributes)
            {
                var keyValuePair = attrib;
                var value        = keyValuePair.Value;
                var key          = keyValuePair.Key;
                if (value is EntityReference entityReference)
                {
                    // Get Lookup attribute meta data for the ER to check for polymorphic relationship.
                    var attributeInfo = mUtil.GetAttributeMetadata(entityName, key.ToLower());
                    if (attributeInfo is Xrm.Sdk.Metadata.LookupAttributeMetadata attribData)
                    {
                        // Now get relationship to make sure we use the correct name.
                        var eData     = mUtil.GetEntityMetadata(Xrm.Sdk.Metadata.EntityFilters.Relationships, entityName);
                        var ERNavName = eData.ManyToOneRelationships.FirstOrDefault(w => w.ReferencingAttribute.Equals(attribData.LogicalName) &&
                                                                                    w.ReferencedEntity.Equals(entityReference.LogicalName))
                                        ?.ReferencingEntityNavigationPropertyName;
                        if (!string.IsNullOrEmpty(ERNavName))
                        {
                            key = ERNavName;
                        }

                        // Populate Key property
                        key = $"{key}@odata.bind";
                    }
                    else if (attributeInfo == null)
                    {
                        // Fault here.
                        throw new CdsClientOperationException($"Entity Reference {key.ToLower()} was not found for entity {entityName}.", null);
                    }

                    string entityReferanceValue = string.Empty;
                    // process ER Value
                    if (entityReference.KeyAttributes?.Any() == true)
                    {
                        entityReferanceValue = ParseAltKeyCollection(entityReference.KeyAttributes);
                    }
                    else
                    {
                        entityReferanceValue = entityReference.Id.ToString();
                    }


                    value = $"/{mUtil.GetEntityMetadata(Xrm.Sdk.Metadata.EntityFilters.Entity, entityReference.LogicalName).EntitySetName}({entityReferanceValue})";
                }
                else
                {
                    key = key.ToLower();
                    if (value is OptionSetValueCollection optionSetValues)
                    {
                        string mselectValueString = string.Empty;
                        foreach (var opt in optionSetValues)
                        {
                            mselectValueString += $"{opt.Value.ToString()},";
                        }
                        value = mselectValueString.Remove(mselectValueString.Length - 1);
                    }
                    else if (value is OptionSetValue optionSetValue)
                    {
                        value = optionSetValue.Value.ToString();
                    }
                    else if (value is DateTime dateTimeValue)
                    {
                        value = dateTimeValue.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
                    }
                    else if (value is Money moneyValue)
                    {
                        value = moneyValue.Value;
                    }
                    else if (value is bool boolValue)
                    {
                        value = boolValue.ToString();
                    }
                    else if (value is Guid guidValue)
                    {
                        value = guidValue.ToString();
                    }
                    else if (value is null)
                    {
                        value = null;
                    }
                }
                expandoObject.Add(key, value);
            }
            return((ExpandoObject)(expandoObject));
        }
Example #4
0
 public DynamicEntityUtility(CdsServiceClient svcActions, MetadataUtility metaUtility)
 {
     svcAct       = svcActions;
     metadataUtil = metaUtility;
 }