示例#1
0
        public void GetOptionsetLabels(Dictionary <string, string[]> entityAttributes, int[] lcids)
        {
            if (entityAttributes.Count == 0)
            {
                return;
            }

            var logicalNames = entityAttributes.Keys.ToArray();
            var attributes   = (from e in entityAttributes
                                from a in e.Value
                                select a).ToArray();

            MetadataQueryBuilder builder = new MetadataQueryBuilder();

            builder.AddEntities(logicalNames, new string[] { "Attributes" });
            builder.AddAttributes(attributes, new string[] { "OptionSet" });

            var response = (RetrieveMetadataChangesResponse)_service.Execute(builder.Request);

            foreach (var metadata in response.EntityMetadata)
            {
                foreach (var attribute in metadata.Attributes)
                {
                    var optionSetMetaData = attribute as PicklistAttributeMetadata;
                    if (optionSetMetaData != null)
                    {
                        // Store the language labels
                        string key = metadata.LogicalName + "." + attribute.LogicalName;
                        _optionSetCache.Add(key, optionSetMetaData);
                    }
                }
            }
        }
示例#2
0
        public string GetOptionsetLabel(string entityLogicalName, string attributeLogicalName, int value, int lcid)
        {
            // Get the cache
            string key = entityLogicalName + "." + attributeLogicalName;// + "." + lcid.ToString();
            PicklistAttributeMetadata optionSetMetaData = null;

            if (!_optionSetCache.ContainsKey(key))
            {
                MetadataQueryBuilder builder = new MetadataQueryBuilder();
                builder.AddEntities(new string[] { entityLogicalName }, new string[] { "Attributes" });
                builder.AddAttributes(new string[] { attributeLogicalName }, new string[] { "OptionSet" });
                //builder.SetLanguage(lcid);
                var response = (RetrieveMetadataChangesResponse)_service.Execute(builder.Request);

                AttributeMetadata metaData = response.EntityMetadata[0].Attributes.Where(a => a.LogicalName == attributeLogicalName).FirstOrDefault();
                optionSetMetaData = metaData as PicklistAttributeMetadata ?? throw new Exception(String.Format("Cannot find metadata for {0}.{1}", entityLogicalName, attributeLogicalName));
                if (optionSetMetaData == null)
                {
                    throw new Exception(String.Format("Attribute is not optionset metadata for {0}.{1}", entityLogicalName, attributeLogicalName));
                }
                _optionSetCache.Add(key, optionSetMetaData);
            }
            else
            {
                optionSetMetaData = _optionSetCache[key];
            }

            var option = optionSetMetaData.OptionSet.Options.Where(o => o.Value == value).FirstOrDefault();

            if (option != null)
            {
                var labelMetadata = option.Label.LocalizedLabels.Where(l => l.LanguageCode == lcid).FirstOrDefault();
                // Fall back on first value if no translation
                if (labelMetadata == null)
                {
                    labelMetadata = option.Label.LocalizedLabels.FirstOrDefault();
                }

                if (labelMetadata == null)
                {
                    throw new Exception(String.Format("Cannot find translation for value {0} of {1}.{2}", value, entityLogicalName, attributeLogicalName));
                }

                return(labelMetadata.Label);
            }

            return(string.Empty);
        }