MS CRM / Разработка

Получение списка значений пиклиста

Получение списка возможных значений атрибута типа Picklist в CRM 2016 и Dynamics 365.

Серверный код

// Значения списка атрибута сущности
{
    var request = new RetrieveAttributeRequest
    {
        EntityLogicalName = "contact",
        LogicalName = "paymenttermscode"
    };
    var response = (RetrieveAttributeResponse)service.Execute(request);
    var attributeMetadata = (PicklistAttributeMetadata)response.AttributeMetadata;
    Console.WriteLine($"Entity {request.EntityLogicalName}, picklist attribute {request.LogicalName}:");
    foreach (var option in attributeMetadata.OptionSet.Options)
        Console.WriteLine($"{option.Value} - {option.Label.UserLocalizedLabel.Label}");
}


// Значения глобального списка
{
    var request = new RetrieveOptionSetRequest
    {
        Name = "budgetstatus"
    };
    var response = (RetrieveOptionSetResponse)service.Execute(request);
    var attributeMetadata = (OptionSetMetadata)response.OptionSetMetadata;
    Console.WriteLine($"Global picklist {request.Name}:");
    foreach (var option in attributeMetadata.Options)
        Console.WriteLine($"{option.Value} - {option.Label.UserLocalizedLabel.Label}");
}

Использование Web API

Запрос

GET http://crmdevapp01/DEV01/api/data/v8.2/EntityDefinitions?$select=MetadataId&$filter=SchemaName eq 'Contact' HTTP/1.1

Ответ

{
  "@odata.context":"http://crmdevapp01/DEV01/api/data/v8.2/$metadata#EntityDefinitions(MetadataId)","value":[
    {
      "MetadataId":"608861bc-50a4-4c5f-a02c-21fe1943e2cf"
    }
  ]
}

Полученный MetadataId используем в следующем запросе

GET http://crmdevapp01/DEV01/api/data/v8.2/EntityDefinitions(608861bc-50a4-4c5f-a02c-21fe1943e2cf)/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=OptionSet,GlobalOptionSet&$filter=LogicalName eq 'paymenttermscode'&$expand=OptionSet,GlobalOptionSet HTTP/1.1

Ответ

{
  "@odata.context":"http://crmdevapp01/DEV01/api/data/v8.2/$metadata#EntityDefinitions(608861bc-50a4-4c5f-a02c-21fe1943e2cf)/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata(OptionSet,GlobalOptionSet)",
  "value":[
    {
      "MetadataId":"384614ff-b79c-4009-9bec-7dedf8c7b99c",
      "OptionSet@odata.context":"http://crmdevapp01/DEV01/api/data/v8.2/$metadata#EntityDefinitions(608861bc-50a4-4c5f-a02c-21fe1943e2cf)/Attributes(384614ff-b79c-4009-9bec-7dedf8c7b99c)/Microsoft.Dynamics.CRM.PicklistAttributeMetadata/OptionSet/$entity",
      "OptionSet":{
        "MetadataId":"7154ab7b-7c99-4712-9d8c-7eaaae4f6f78",
        "HasChanged":null,
        "Description":{
          "LocalizedLabels":[ { "Label":"\u0423\u0441\u043b\u043e\u0432\u0438\u044f \u043e\u043f\u043b\u0430\u0442\u044b \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u0430.", "LanguageCode":1049, "IsManaged":true, "MetadataId":"93680bd0-1882-43bb-9970-6a58ecf33e70", "HasChanged":null } ],
          "UserLocalizedLabel":{ "Label":"\u0423\u0441\u043b\u043e\u0432\u0438\u044f \u043e\u043f\u043b\u0430\u0442\u044b \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u0430.", "LanguageCode":1049, "IsManaged":true, "MetadataId":"93680bd0-1882-43bb-9970-6a58ecf33e70", "HasChanged":null }
        },
        "DisplayName":{
          "LocalizedLabels":[ { "Label":"\u0423\u0441\u043b\u043e\u0432\u0438\u044f \u043e\u043f\u043b\u0430\u0442\u044b", "LanguageCode":1049, "IsManaged":true, "MetadataId":"b40d245c-e6b2-4184-b0e4-a0d6cd3bfd02", "HasChanged":null } ],
          "UserLocalizedLabel":{ "Label":"\u0423\u0441\u043b\u043e\u0432\u0438\u044f \u043e\u043f\u043b\u0430\u0442\u044b", "LanguageCode":1049, "IsManaged":true, "MetadataId":"b40d245c-e6b2-4184-b0e4-a0d6cd3bfd02", "HasChanged":null }
        },
        "IsCustomOptionSet":false,
        "IsGlobal":false,
        "IsManaged":true,
        "IsCustomizable":{ "Value":true, "CanBeChanged":false, "ManagedPropertyLogicalName":"iscustomizable" },
        "Name":"contact_paymenttermscode",
        "OptionSetType":"Picklist",
        "IntroducedVersion":null,
        "Options":[
          {
            "Value":33,
            "Label":{
              "LocalizedLabels":[ { "Label":"100% \u0430\u0432\u0430\u043d\u0441\u043e\u0432\u044b\u0439 \u043f\u043b\u0430\u0442\u0435\u0436%","LanguageCode":1049,"IsManaged":true,"MetadataId":"74ad31c1-35d2-43f3-99ba-8621220c373a","HasChanged":null } ],
              "UserLocalizedLabel":{ "Label":"100% \u0430\u0432\u0430\u043d\u0441\u043e\u0432\u044b\u0439 \u043f\u043b\u0430\u0442\u0435\u0436%","LanguageCode":1049,"IsManaged":true,"MetadataId":"74ad31c1-35d2-43f3-99ba-8621220c373a","HasChanged":null }
            },
            "Description":{
              "LocalizedLabels":[],
              "UserLocalizedLabel":null
            },
            "Color":null,
            "IsManaged":true,
            "MetadataId":null,
            "HasChanged":null
          },
          {
            "Value":34,
            "Label":{
              "LocalizedLabels":[ { "Label":"50% \u0430\u0432\u0430\u043d\u0441\u043e\u0432\u044b\u0439 \u043f\u043b\u0430\u0442\u0435\u0436","LanguageCode":1049,"IsManaged":true,"MetadataId":"75a58df4-be6e-48f0-a037-ce8e4ac0e6fc","HasChanged":null } ],
              "UserLocalizedLabel":{ "Label":"50% \u0430\u0432\u0430\u043d\u0441\u043e\u0432\u044b\u0439 \u043f\u043b\u0430\u0442\u0435\u0436","LanguageCode":1049,"IsManaged":true,"MetadataId":"75a58df4-be6e-48f0-a037-ce8e4ac0e6fc","HasChanged":null }
            },
            "Description":{
              "LocalizedLabels":[],
              "UserLocalizedLabel":null
            },
            "Color":null,
            "IsManaged":true,
            "MetadataId":null,
            "HasChanged":null
          },
          {
            "Value":35,
            "Label":{
              "LocalizedLabels":[ { "Label":"\u041e\u043f\u043b\u0430\u0442\u0430 \u043f\u043e \u0444\u0430\u043a\u0442\u0443","LanguageCode":1049,"IsManaged":true,"MetadataId":"d4a5bacf-319a-40fa-80d1-8e4e920e5da4","HasChanged":null } ],
              "UserLocalizedLabel":{ "Label":"\u041e\u043f\u043b\u0430\u0442\u0430 \u043f\u043e \u0444\u0430\u043a\u0442\u0443","LanguageCode":1049,"IsManaged":true,"MetadataId":"d4a5bacf-319a-40fa-80d1-8e4e920e5da4","HasChanged":null }
            },
            "Description":{
              "LocalizedLabels":[],
              "UserLocalizedLabel":null
            },
            "Color":null,
            "IsManaged":true,
            "MetadataId":null,
            "HasChanged":null
          }
        ]
      },
      "GlobalOptionSet":null
    }
  ]
}

Исходный код

About the author

Добавить комментарий