示例#1
0
        public static void Should_Retrieve_A_Correct_Entity_By_Alternate_Key()
        {
            var fakedContext    = new XrmFakedContext();
            var accountMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityMetadata();

            accountMetadata.LogicalName = Account.EntityLogicalName;
            var alternateKeyMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata();

            alternateKeyMetadata.KeyAttributes = new string[] { "alternateKey" };
            accountMetadata.SetFieldValue("_keys", new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata[]
            {
                alternateKeyMetadata
            });
            fakedContext.InitializeMetadata(accountMetadata);
            var account = new Entity(Account.EntityLogicalName);

            account.Id = Guid.NewGuid();
            account.Attributes.Add("alternateKey", "key");
            fakedContext.Initialize(account);
            var fakedService = fakedContext.GetOrganizationService();

            var request = new RetrieveRequest
            {
                Target    = new EntityReference(Account.EntityLogicalName, "alternateKey", "key"),
                ColumnSet = new ColumnSet(allColumns: true)
            };

            var retrievedAccount = (RetrieveResponse)fakedService.Execute(request);

            Assert.Equal(account.Id, retrievedAccount.Entity.Id);
        }
        public void When_updating_an_entity_by_alternate_key_the_context_should_reflect_changes()
        {
            var context = new XrmFakedContext();
            var service = context.GetOrganizationService();

            var accountMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityMetadata();

            accountMetadata.LogicalName = Account.EntityLogicalName;
            var alternateKeyMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata();

            alternateKeyMetadata.KeyAttributes = new string[] { "AccountNumber" };
            accountMetadata.SetFieldValue("_keys", new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata[]
            {
                alternateKeyMetadata
            });
            context.InitializeMetadata(accountMetadata);

            var e = new Entity("account");

            e["AccountNumber"] = 9000;
            e["name"]          = "Before update";
            var guid = service.Create(e);

            Assert.Equal(context.Data["account"][guid]["name"], "Before update");

            //now update the name
            e         = new Entity("account", "AccountNumber", 9000);
            e["name"] = "After update";
            service.Update(e);

            Assert.Equal(context.Data["account"][guid]["name"], "After update");
        }
        /// <summary>
        /// Gets the option sets.
        /// </summary>
        /// <param name="entityName">Name of the entity.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <returns>Result.</returns>
        public Result GetOptionSets(string entityName, string attributeName)
        {
            try
            {
                logSystem.CreateLog(System.Reflection.MethodBase.GetCurrentMethod().ToString() + " is called");

                string AttributeName     = attributeName;
                string EntityLogicalName = entityName;
                RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName   = EntityLogicalName
                };
                RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)xrmService.Execute(retrieveDetails);
                Microsoft.Xrm.Sdk.Metadata.EntityMetadata            metadata         = retrieveEntityResponseObj.EntityMetadata;
                Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals
                                                                                                                               (attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;

                foreach (OptionMetadata item in picklistMetadata.OptionSet.Options)
                {
                    ObjectCarrier.SetValue(item.Value.ToString(), item.Label.UserLocalizedLabel.Label);
                }

                return(new Result(false, string.Empty, ObjectCarrier.GetAllValues(), logSystem));
            }
            catch (Exception ex)
            {
                return(new Result(true,
                                  MethodBase.GetCurrentMethod().ToString() + " : " + ExceptionHandler.HandleException(ex),
                                  null, logSystem));
            }
        }
        /// <summary>
        /// Gets the option set text.
        /// </summary>
        /// <param name="entityName">Name of the entity.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="optionSetValue">The option set value.</param>
        /// <returns>Result.</returns>
        public Result GetOptionSetText(string entityName, string attributeName, int?optionSetValue)
        {
            try
            {
                logSystem.CreateLog(System.Reflection.MethodBase.GetCurrentMethod().ToString() + " is called");

                string AttributeName     = attributeName;
                string EntityLogicalName = entityName;
                RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName   = EntityLogicalName
                };
                RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)xrmService.Execute(retrieveDetails);
                Microsoft.Xrm.Sdk.Metadata.EntityMetadata            metadata         = retrieveEntityResponseObj.EntityMetadata;
                Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals
                                                                                                                               (attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
                Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;
                IList <OptionMetadata> OptionsList = (from o in options.Options
                                                      where o.Value.Value == optionSetValue
                                                      select o).ToList();
                string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
                return(new Result(false, string.Empty, optionsetLabel, logSystem));
            }
            catch (Exception ex)
            {
                return(new Result(true,
                                  MethodBase.GetCurrentMethod().ToString() + " : " + ExceptionHandler.HandleException(ex),
                                  null, logSystem));
            }
        }
示例#5
0
        /// <summary>
        /// Obtiene el texto del item de un campo picklist.
        /// </summary>
        /// <param name="entityName">NOmbre de la entidad donde se encuentra el campo</param>
        /// <param name="attributeName">Nombre del campo tipo picklist</param>
        /// <param name="optionSetValue">Valor del picklist </param>
        /// <param name="service">Objeto de servicio.</param>
        /// <returns></returns>
        public static string GetPickListText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
        {
            string entityLogicalName = entityName;
            RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.All,
                LogicalName   = entityLogicalName
            };
            RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);

            Microsoft.Xrm.Sdk.Metadata.EntityMetadata            metadata         = retrieveEntityResponseObj.EntityMetadata;
            Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
            Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata         options          = picklistMetadata.OptionSet;
            IList <OptionMetadata> picklistOption = (from o in options.Options
                                                     where o.Value.Value == optionSetValue
                                                     select o).ToList();
            string picklistLabel = (picklistOption.First()).Label.UserLocalizedLabel.Label;

            return(picklistLabel);
        }
示例#6
0
        public List <CrmPicklist> ObtenerOptionSetEntidad(string NombreEntidad, string NombreAtributo)
        {
            List <CrmPicklist> lista = null;

            RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.All,
                LogicalName   = NombreEntidad
            };
            RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)_servicio.Execute(retrieveDetails);

            Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;

            Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata =
                metadata.Attributes.FirstOrDefault(
                    attribute => String.Equals(attribute.LogicalName, NombreAtributo, StringComparison.OrdinalIgnoreCase)
                    ) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;

            Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;

            IList <OptionMetadata> OptionsList = (from o in options.Options
                                                  select o).ToList();


            if (OptionsList != null)
            {
                lista = new List <CrmPicklist>();
                foreach (OptionMetadata option in OptionsList)
                {
                    CrmPicklist item = new CrmPicklist();

                    item.Id     = option.Value;
                    item.Nombre = option.Label.UserLocalizedLabel.Label;
                    lista.Add(item);
                }
            }


            return(lista);
        }
        public static EntityMetadata ToEntityMetadata(this EntityDefinition entityDefinition)
        {
            var metadata = new EntityMetadata
            {
                LogicalName           = entityDefinition.EntityName,
                LogicalCollectionName = entityDefinition.EntityCollectionName,
                IsActivity            = entityDefinition.PrimaryIdAttributeName == "activityid",
            };

            foreach (var attributeName in entityDefinition.AttributeNames)
            {
                AttributeMetadata attribute = null;

                switch (entityDefinition.GetAttributeType(attributeName))
                {
                case AttributeTypeCode.Boolean:
                    attribute = new BooleanAttributeMetadata();
                    break;

                case AttributeTypeCode.Customer:
                case AttributeTypeCode.Owner:
                case AttributeTypeCode.Lookup:
                    var lookups = entityDefinition.GetCrmLookupAttributes(attributeName);
                    attribute = new LookupAttributeMetadata
                    {
                        Targets = lookups.Select(l => l.TargetEntityName).Distinct().ToArray()
                    };
                    break;

                case AttributeTypeCode.DateTime:
                    DateTimeBehavior behavior;
                    switch (entityDefinition.GetDateTimeBehavior(attributeName))
                    {
                    case Model.DateTimeBehavior.UserLocal:
                        behavior = DateTimeBehavior.UserLocal;
                        break;

                    case Model.DateTimeBehavior.DateOnly:
                        behavior = DateTimeBehavior.DateOnly;
                        break;

                    case Model.DateTimeBehavior.TimeZoneIndependent:
                        behavior = DateTimeBehavior.TimeZoneIndependent;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    attribute = new DateTimeAttributeMetadata
                    {
                        DateTimeBehavior = behavior
                    };
                    break;

                case AttributeTypeCode.Decimal:
                    attribute = new DecimalAttributeMetadata
                    {
                    };
                    break;

                case AttributeTypeCode.Double:
                    break;

                case AttributeTypeCode.Integer:
                    break;

                case AttributeTypeCode.Memo:
                    break;

                case AttributeTypeCode.Money:
                    break;

                case AttributeTypeCode.PartyList:
                    break;

                case AttributeTypeCode.Picklist:
                    break;

                case AttributeTypeCode.State:
                    break;

                case AttributeTypeCode.Status:
                    break;

                case AttributeTypeCode.String:
                    break;

                case AttributeTypeCode.Uniqueidentifier:
                    break;

                case AttributeTypeCode.CalendarRules:
                    break;

                case AttributeTypeCode.Virtual:
                    break;

                case AttributeTypeCode.BigInt:
                    break;

                case AttributeTypeCode.ManagedProperty:
                    break;

                case AttributeTypeCode.EntityName:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                if (attribute != null)
                {
                    metadata.SetAttribute(attribute);
                }
            }

            return(metadata);
        }