如何使用crm sdk和C#从CRM 2011中的实体字段中获取选项集

时间:2014-04-27 13:36:57

标签: c# dynamics-crm-2011 crm

如何使用crm sdk和C#从CRM 2011中的实体字段中获取选项集?我只想与您分享一个直接的方法来获取实体中的字段选项集。

5 个答案:

答案 0 :(得分:11)

在Dynamics CRM中检索元数据信息的正确方法是仅检索所需的信息。我们应该只根据原始问题检索选项集值。当所有需求指定的是选项集的值时,检索实体的所有元数据是不必要的,并且会产生不必要的开销。

以下是获取选项集选项列表的正确方法。

    public static void GetOptionSet(string entityName, string fieldName, IOrganizationService service)
    {

        var attReq = new RetrieveAttributeRequest();
        attReq.EntityLogicalName = entityName;
        attReq.LogicalName = fieldName;
        attReq.RetrieveAsIfPublished = true;

        var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
        var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;

        var optionList = (from o in attMetadata.OptionSet.Options
            select new {Value = o.Value, Text = o.Label.UserLocalizedLabel.Label}).ToList();


    }

答案 1 :(得分:1)

此方法需要实体名称,包含选项集的字段名称以及实例化的IOrganizationService。

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

    public void GetOptionSet(string entityName, string fieldName, IOrganizationService service)
            {
                RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest();
                retrieveDetails.EntityFilters = EntityFilters.All;
                retrieveDetails.LogicalName = entityName;

                RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
                EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
                PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, fieldName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
                OptionSetMetadata options = picklistMetadata.OptionSet;
                var optionlist = (from o in options.Options
                                   select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList();

                //from here you can do anything you want now with the optionlist

            }

参考:
http://guruprasadcrm.blogspot.ae/2011/12/retrieve-optionset-text-in-crm-2011.html

我希望这能帮助你们中的一些人完成你的项目。

答案 2 :(得分:0)

嘿,为什么不用这个?

OptionSetValue CountryOptionSet = Contact.Attributes.Contains("gr_address2_country") ? Contact["gr_address2_country"] as OptionSetValue : null;
 if (CountryOptionSet != null)
                     string Country = CountryOptionSet.Value.ToString();

答案 3 :(得分:0)

取决于它是本地的还是全球的。如果它是本地的那么:

string optionsetText = entity.FormattedValues["new_optionset"];

如果它是全局的那么你需要更多的代码:

        public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
    {
        string AttributeName = attributeName;
        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> OptionsList = (from o in options.Options
                                             where o.Value.Value == optionSetValue
                                             select o).ToList();
        string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
        return optionsetLabel;

我从Guido得到了另一个类似的问题,但是rcados缺少第三个参数。要获取您刚刚设置的文本

var foo = GetoptionsetText("lead", "picklist", 1234 , service)

假设您已经声明了IorganizationService

答案 4 :(得分:0)

获取OptionSet文本的方法有两种:

首先:

OptionSetValue opProductType = new OptionSetValue();
opProductType = (OptionSetValue)item.Attributes[attributeName];
var optionValue = opProductType.Value;

第二

var StatusString = TermAndCon.FormattedValues[attributeName].ToString();

(设置)发布OptionSet值:

newSalesOrder[attributeName] = new OptionSetValue(Convert.ToInt32(optionValue));