Ejemplo n.º 1
0
        public static async Task <Guid> CopyEntityAttribute(this CRMWebAPI api, Guid fromEntityID, Guid toEntityID, Guid fromAttributeID, string attributeType, string logicalName)
        {
            var ec = "EntityDefinitions(" + fromEntityID.ToString() + ")/Attributes(" + fromAttributeID + ")";

            if (attributeType == "Boolean")
            {
                ec += "/Microsoft.Dynamics.CRM.BooleanAttributeMetadata?$expand=OptionSet";
            }
            if (attributeType == "Picklist")
            {
                ec += "/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$expand=OptionSet,GlobalOptionSet";
            }

            dynamic fromAttrib = await api.Get(ec, Guid.Empty);

            IDictionary <string, object> fromAttribValues = (IDictionary <string, object>)fromAttrib;

            fromAttribValues.Remove("MetadataId");
            fromAttribValues.Remove("EntityLogicalName");
            if (attributeType == "Boolean")
            {
                fromAttribValues["@odata.type"] = "Microsoft.Dynamics.CRM.BooleanAttributeMetadata";
                fromAttribValues.Remove("*****@*****.**");
                if (fromAttrib.OptionSet != null)
                {
                    IDictionary <string, object> fromOptionSetValues = (IDictionary <string, object>)fromAttrib.OptionSet;
                    fromOptionSetValues.Remove("Name");
                    fromOptionSetValues.Remove("MetadataId");
                    fromOptionSetValues.Remove("MetadataId");
                    fromOptionSetValues["IsCustomOptionSet"] = true;
                }
            }
            if (attributeType == "Picklist")
            {
                fromAttribValues["@odata.type"] = "Microsoft.Dynamics.CRM.PicklistAttributeMetadata";


                if (fromAttrib.OptionSet != null)
                {
                    IDictionary <string, object> fromOptionSetValues = (IDictionary <string, object>)fromAttrib.OptionSet;
                    fromOptionSetValues.Remove("Name");
                    fromOptionSetValues.Remove("MetadataId");
                    fromOptionSetValues.Remove("MetadataId");
                    fromOptionSetValues["IsCustomOptionSet"] = true;
                }
                else
                {
                    fromAttribValues.Remove("OptionSet");
                    fromAttribValues["*****@*****.**"] = "/GlobalOptionSetDefinitions(" + fromAttrib.GlobalOptionSet.MetadataId + ")";
                    fromAttribValues.Remove("*****@*****.**");
                    fromAttribValues.Remove("GlobalOptionSet");
                }
            }
            fromAttrib.LogicalName = logicalName;
            fromAttrib.SchemaName  = logicalName;


            return(await api.Create("EntityDefinitions(" + toEntityID.ToString() + ")/Attributes", fromAttrib));
        }
Ejemplo n.º 2
0
        public static async Task <ExpandoObject> GetOptionSetByName(this CRMWebAPI api, string optionSetName)
        {
            CRMGetListOptions options = new CRMGetListOptions()
            {
                Select = new[] { "Name" }
            };

            var queryResult = await api.GetList("GlobalOptionSetDefinitions", options);

            foreach (dynamic optionSet in queryResult.List)
            {
                if ((optionSet != null) && (optionSet.Name == optionSetName))
                {
                    var matchingOptionSet = await api.Get("GlobalOptionSetDefinitions", Guid.Parse(optionSet.MetadataId));

                    return(matchingOptionSet);
                }
            }

            return(null);
        }