private static Microsoft.Xrm.Sdk.Metadata.OptionMetadataCollection GetOptionMetadata(string option)
        {
            OptionMetadataCollection optionMetadataCollection = new OptionMetadataCollection();

            if (option != "")
            {
                if (option.Contains("|"))
                {
                    string[] optionArray = option.Split('|');
                    if (optionArray != null && optionArray.Length > 0)
                    {
                        for (int arrayCounter = 0; arrayCounter < optionArray.Length; arrayCounter++)
                        {
                            optionMetadataCollection.Add(new OptionMetadata(
                                                             new Microsoft.Xrm.Sdk.Label(optionArray[arrayCounter], _languageCode), null));
                        }
                    }
                }
                else
                {
                    optionMetadataCollection.Add(new OptionMetadata(
                                                     new Microsoft.Xrm.Sdk.Label(option, _languageCode), null));
                }
            }
            return(optionMetadataCollection);
        }
        public void GetOptionSetNameFromValueOk()
        {
            var optionSetName = "name";
            var value         = 12;
            var name          = "value";

            OrganizationService.Setup(s => s.Execute(It.IsAny <OrganizationRequest>())).Callback <OrganizationRequest>(request =>
            {
                Assert.IsInstanceOfType(request, typeof(RetrieveOptionSetRequest));
                var r = (RetrieveOptionSetRequest)request;

                Assert.AreEqual(r.Name, optionSetName);
            }).Returns(() =>
            {
                var options = new OptionMetadataCollection();
                options.Add(new OptionMetadata(new Label(new LocalizedLabel("autre", 1033), null), 18));
                options.Add(new OptionMetadata(new Label(new LocalizedLabel(name, 1033), null), value));

                var response = new RetrieveOptionSetResponse();
                response.Results["OptionSetMetadata"] = new OptionSetMetadata(options);
                return(response);
            });

            var result = Service.GetOptionSetNameFromValue(optionSetName, value);

            Assert.AreEqual(result, name);
        }
        /// <summary>
        /// Procedure: CreateGlobalOptionSet
        /// Handles:
        /// Created By: Will Wilson
        /// Created Date: 01/01/2016
        /// Changes By:
        /// Changes Date:
        /// Changes Made:
        /// </summary>
        public void CreateGlobalOptionSet(IOrganizationService service, string solutionName, string schema, string label, int lang, string[] optionLabels)
        {
            OptionMetadataCollection options = new OptionMetadataCollection();

            foreach (string o in optionLabels)
            {
                options.Add(new OptionMetadata(new Label(o, lang), null));
            }

            CreateOptionSetRequest req = new CreateOptionSetRequest()
            {
                SolutionUniqueName = solutionName,
                OptionSet          = new OptionSetMetadata(options)
                {
                    Name          = schema,
                    DisplayName   = new Label(label, lang),
                    IsGlobal      = true,
                    OptionSetType = OptionSetType.Picklist,
                }
            };

            CreateOptionSetResponse res = (CreateOptionSetResponse)service.Execute(req);

            Console.WriteLine("Global OptionSet created: {0}", schema);
        }
        public PicklistAttributeMetadata CreateOptionSet(string schema, string label, int lang, AttributeRequiredLevel requiredLevel, string[] optionLabels)
        {
            OptionMetadataCollection options = new OptionMetadataCollection();

            foreach (string o in optionLabels)
            {
                options.Add(new OptionMetadata(new Label(o, lang), null));
            }
            OptionSetMetadata optMetadata = new OptionSetMetadata(options)
            {
                IsGlobal      = false,
                OptionSetType = OptionSetType.Picklist,
            };

            PicklistAttributeMetadata pickListAttribute =
                new PicklistAttributeMetadata
            {
                // Set base properties
                SchemaName    = schema,
                DisplayName   = new Label(label, lang),
                RequiredLevel = new AttributeRequiredLevelManagedProperty(requiredLevel),
                //Description = new Label("Picklist Attribute", lang),
                // Set extended properties
                // Build local picklist options
                OptionSet = optMetadata
            };

            return(pickListAttribute);
        }
        private IOrganizationService SetupIOrganisationService(Entity recordContext, int optionSetValue)
        {
            StubIOrganizationService stubIOrganisationService = new StubIOrganizationService();

            stubIOrganisationService.RetrieveStringGuidColumnSet = (entityLogicalName, recordId, columnSet)
                                                                   =>
            {
                if (entityLogicalName == "dxtools_payment")
                {
                    return(recordContext);
                }
                else
                {
                    return(null);
                }
            };

            stubIOrganisationService.ExecuteOrganizationRequest = (request)
                                                                  =>
            {
                if (request.RequestName == "RetrieveAttribute")
                {
                    RetrieveAttributeResponse response = new RetrieveAttributeResponse();
                    return(response);
                }
                else
                {
                    return(null);
                }
            };

            Microsoft.Xrm.Sdk.Messages.Fakes.ShimRetrieveAttributeResponse.AllInstances.AttributeMetadataGet = (i)
                                                                                                               =>
            {
                OptionMetadataCollection optionMetadataCollection = new OptionMetadataCollection();
                OptionMetadata           optionMetadata           = new OptionMetadata(new Label(), optionSetValue);
                optionMetadata.Label.UserLocalizedLabel = new LocalizedLabel("IN", 1033);
                optionMetadataCollection.Add(optionMetadata);

                PicklistAttributeMetadata picklistAttributeMetadata = new PicklistAttributeMetadata();
                picklistAttributeMetadata.OptionSet = new OptionSetMetadata(optionMetadataCollection);
                return(picklistAttributeMetadata);
            };

            return(stubIOrganisationService);
        }
        private void ComponentTypeSelector_Load(object sender, EventArgs e)
        {
            if (_omc.All(o => o.Value != 80))
            {
                var lb = new Label {
                    UserLocalizedLabel = new LocalizedLabel("Model driven app", -1)
                };
                _omc.Add(new OptionMetadata(lb, 80));
            }

            foreach (var omd in _omc)
            {
                lvTypes.Items.Add(new ListViewItem(omd.Label?.UserLocalizedLabel?.Label)
                {
                    Tag = omd.Value, Checked = true
                });
            }
        }
Exemple #7
0
        protected OptionMetadataCollection ParseOptions(string options)
        {
            var result = new OptionMetadataCollection();

            foreach (var option in options.Split(';'))
            {
                var tokens = option.Split('|');
                if (tokens.Length > 1)
                {
                    var metadata = new OptionMetadata
                    {
                        Label = new Label(tokens[0], LcId),
                        Value = Convert.ToInt32(tokens[1])
                    };
                    result.Add(metadata);
                }
            }
            return(result);
        }
        private void CreateProperty(string entityName, Type propertyType, IEnumerable <object> attributes, string propertyName, PropertyInfoAttribute info)
        {
            var createAttributeRequest = new CreateAttributeRequest()
            {
                EntityName = entityName
            };

            if (propertyType == typeof(string))
            {
                var attrInfo = attributes.SingleOrDefault(attr => attr.GetType() == typeof(StringAttribute));
                if (attrInfo != null)
                {
                    var stringInfo = (StringAttribute)attrInfo;

                    createAttributeRequest.Attribute = new StringAttributeMetadata
                    {
                        SchemaName    = propertyName,
                        RequiredLevel = new AttributeRequiredLevelManagedProperty(info.AttributeRequiredLevel),
                        DisplayName   = info.DisplayName.ToLabel(),
                        Description   = info.Description.ToLabel(),
                        MaxLength     = stringInfo.MaxLength,
                        FormatName    = stringInfo.StringFormat
                    };

                    service.Execute(createAttributeRequest);
                }
            }
            else if (propertyType == typeof(bool))
            {
                var attrInfo = attributes.SingleOrDefault(attr => attr.GetType() == typeof(BooleanAttribute));
                if (attrInfo != null)
                {
                    var booleanAttr = (BooleanAttribute)attrInfo;
                    createAttributeRequest.Attribute = new BooleanAttributeMetadata
                    {
                        SchemaName    = propertyName,
                        RequiredLevel = new AttributeRequiredLevelManagedProperty(info.AttributeRequiredLevel),
                        DisplayName   = info.DisplayName.ToLabel(),
                        Description   = info.Description.ToLabel(),
                        OptionSet     = new BooleanOptionSetMetadata(
                            new OptionMetadata("True".ToLabel(), 1),
                            new OptionMetadata("False".ToLabel(), 0)),
                    };
                }

                service.Execute(createAttributeRequest);
            }
            else if (propertyType == typeof(DateTime))
            {
                var attrInfo = attributes.SingleOrDefault(attr => attr.GetType() == typeof(DateTimeAttribute));
                if (attrInfo != null)
                {
                    var dateTimeInfo = (DateTimeAttribute)attrInfo;

                    createAttributeRequest.Attribute = new DateTimeAttributeMetadata
                    {
                        SchemaName    = propertyName,
                        RequiredLevel = new AttributeRequiredLevelManagedProperty(info.AttributeRequiredLevel),
                        DisplayName   = info.DisplayName.ToLabel(),
                        Description   = info.Description.ToLabel(),
                        Format        = dateTimeInfo.Format,
                        ImeMode       = dateTimeInfo.ImeMode
                    };
                }

                service.Execute(createAttributeRequest);
            }
            else if (propertyType == typeof(decimal))
            {
                var attrInfo = attributes.SingleOrDefault(attr => attr.GetType() == typeof(DecimalAttribute));
                if (attrInfo != null)
                {
                    var decimalInfo = (DecimalAttribute)attrInfo;

                    createAttributeRequest.Attribute = new DecimalAttributeMetadata
                    {
                        SchemaName    = propertyName,
                        RequiredLevel = new AttributeRequiredLevelManagedProperty(info.AttributeRequiredLevel),
                        DisplayName   = info.DisplayName.ToLabel(),
                        Description   = info.Description.ToLabel(),
                        MaxValue      = decimalInfo.MaxValue,
                        MinValue      = decimalInfo.MinValue,
                        Precision     = decimalInfo.Precision
                    };
                }

                service.Execute(createAttributeRequest);
            }
            else if (propertyType == typeof(double))
            {
                var attrInfo = attributes.SingleOrDefault(attr => attr.GetType() == typeof(DoubleAttribute));
                if (attrInfo != null)
                {
                    var decimalInfo = (DoubleAttribute)attrInfo;

                    createAttributeRequest.Attribute = new DoubleAttributeMetadata
                    {
                        SchemaName    = propertyName,
                        RequiredLevel = new AttributeRequiredLevelManagedProperty(info.AttributeRequiredLevel),
                        DisplayName   = info.DisplayName.ToLabel(),
                        Description   = info.Description.ToLabel(),
                        MaxValue      = decimalInfo.MaxValue,
                        MinValue      = decimalInfo.MinValue,
                        Precision     = decimalInfo.Precision
                    };
                }

                service.Execute(createAttributeRequest);
            }
            else if (propertyType == typeof(int))
            {
                var attrInfo = attributes.SingleOrDefault(attr => attr.GetType() == typeof(IntegerAttribute));
                if (attrInfo != null)
                {
                    var integerInfo = (IntegerAttribute)attrInfo;

                    createAttributeRequest.Attribute = new IntegerAttributeMetadata
                    {
                        SchemaName    = propertyName,
                        RequiredLevel = new AttributeRequiredLevelManagedProperty(info.AttributeRequiredLevel),
                        DisplayName   = info.DisplayName.ToLabel(),
                        Description   = info.Description.ToLabel(),
                        MaxValue      = integerInfo.MaxValue,
                        MinValue      = integerInfo.MinValue,
                        Format        = integerInfo.Format
                    };
                }

                service.Execute(createAttributeRequest);
            }
            else if (propertyType.IsEnum)
            {
                var attrInfo = attributes.SingleOrDefault(attr => attr.GetType() == typeof(OptionSetAttribute));
                if (attrInfo != null)
                {
                    var options = new OptionMetadataCollection();
                    foreach (var value in Enum.GetValues(propertyType))
                    {
                        options.Add(new OptionMetadata(value.ToString().ToLabel(), (int)value));
                    }

                    createAttributeRequest.Attribute = new PicklistAttributeMetadata
                    {
                        SchemaName    = propertyName,
                        RequiredLevel = new AttributeRequiredLevelManagedProperty(info.AttributeRequiredLevel),
                        DisplayName   = info.DisplayName.ToLabel(),
                        Description   = info.Description.ToLabel(),
                        OptionSet     = new OptionSetMetadata(options)
                        {
                            IsGlobal          = false,
                            DisplayName       = info.DisplayName.ToLabel(),
                            Description       = info.Description.ToLabel(),
                            IsCustomOptionSet = true,
                            OptionSetType     = OptionSetType.Picklist,
                            Name = propertyName
                        }
                    };

                    service.Execute(createAttributeRequest);
                }
            }
        }
 private OptionMetadataCollection GetOptionMetadataCollection(AttributeTemplate attributeTemplate)
 {
     var optionMetadataCollection = new OptionMetadataCollection();
     if (attributeTemplate.OptionSetList != null)
     {
         foreach (var optionMetadataTemplate in attributeTemplate.OptionSetList)
         {
             var optionMetadata = new OptionMetadata
             {
                 Description = GetLabelWithLocalized(optionMetadataTemplate.Description),
                 Label = GetLabelWithLocalized(optionMetadataTemplate.Label),
                 Value = optionMetadataTemplate.Value
             };
             optionMetadataCollection.Add(optionMetadata);
         }
     }
     return optionMetadataCollection;
 }