CRM Dynamics 365:如何通过 REST API 获取非全局选项集值元数据?

CRM Dynamics 365: How to get the non global optionset values metadata via REST API?

Dynamics 365 v9.1.
如何通过 REST API?

获取非全局选项集值元数据

我的查询:https://mycompany.com/MyCompany/api/data/v9.1/EntityDefinitions(LogicalName='lead')/Attributes(LogicalName='statuscode')

我的 C# 代码:

public static HttpClient CreateCrmHttpClient(string domain, string crmWebApiUrl, string authType, string crmLogin, 
    string crmPassword, Guid? callerId)
{
    var uri = new Uri(crmWebApiUrl);
    var credentialsCache = new CredentialCache
        {{uri, authType, new NetworkCredential(crmLogin, crmPassword, domain)}};

    var handler = new HttpClientHandler {Credentials = credentialsCache};

    var httpClient = new HttpClient(handler) {BaseAddress = uri, Timeout = new TimeSpan(0, 0, 60)};

    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
    httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
    httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations=\"*\"");

    if (callerId != null)
    {
        httpClient.DefaultRequestHeaders.Add("MSCRMCallerID", callerId.Value.ToString());
    }
    return httpClient;
}

...

public string RetrieveAttributeMetadata(string entityName, string attributeName)
{
    var httpClient = CrmClientHelper.CreateCrmHttpClient(Domain, CrmWebApiUrl, AuthType, CrmLogin,
        CrmPassword, CallerId);
    var query = $"EntityDefinitions(LogicalName='{entityName}')/Attributes(LogicalName='{attributeName}')";
    var response = httpClient.GetAsync(query).Result;
    return response.Content.ReadAsStringAsync().Result;
}

低于结果,但它不包含 OptionSet 值元数据。获取方式:

{
"@odata.context": "https://mycompany.com/MyCompany/api/data/v9.1/$metadata#EntityDefinitions('lead')/Attributes/Microsoft.Dynamics.CRM.StatusAttributeMetadata/$entity",
"@odata.type": "#Microsoft.Dynamics.CRM.StatusAttributeMetadata",
"DefaultFormValue": -1,
"AttributeOf": null,
"AttributeType": "Status",
"ColumnNumber": 54,
"DeprecatedVersion": null,
"IntroducedVersion": "5.0.0.0",
"EntityLogicalName": "lead",
"IsCustomAttribute": false,
"IsPrimaryId": false,
"IsValidODataAttribute": true,
"IsPrimaryName": false,
"IsValidForCreate": true,
"IsValidForRead": true,
"IsValidForUpdate": true,
"CanBeSecuredForRead": false,
"CanBeSecuredForCreate": false,
"CanBeSecuredForUpdate": false,
"IsSecured": false,
"IsRetrievable": true,
"IsFilterable": false,
"IsSearchable": false,
"IsManaged": true,
"LinkedAttributeId": null,
"LogicalName": "statuscode",
"IsValidForForm": true,
"IsRequiredForForm": false,
"IsValidForGrid": true,
"SchemaName": "StatusCode",
"ExternalName": null,
"IsLogical": false,
"IsDataSourceSecret": false,
"InheritsFrom": null,
"CreatedOn": "1900-01-01T00:00:00+03:00",
"ModifiedOn": "2022-04-23T06:20:18+03:00",
"SourceType": null,
"AutoNumberFormat": null,
"MetadataId": "ed2b31c0-723f-447b-ac19-7fee763f3c8c",
"HasChanged": null,
"AttributeTypeName": {
"Value": "StatusType"
},
"Description": {
"LocalizedLabels": [
{
"Label": "Выберите состояние интереса.",
"LanguageCode": 1049,
"IsManaged": true,
"MetadataId": "8999f6ca-2241-db11-898a-0007e9e17ebd",
"HasChanged": null
}
],
"UserLocalizedLabel": {
"Label": "Выберите состояние интереса.",
"LanguageCode": 1049,
"IsManaged": true,
"MetadataId": "8999f6ca-2241-db11-898a-0007e9e17ebd",
"HasChanged": null
}
},
"DisplayName": {
"LocalizedLabels": [
{
"Label": "Причина состояния",
"LanguageCode": 1049,
"IsManaged": true,
"MetadataId": "8899f6ca-2241-db11-898a-0007e9e17ebd",
"HasChanged": null
}
],
"UserLocalizedLabel": {
"Label": "Причина состояния",
"LanguageCode": 1049,
"IsManaged": true,
"MetadataId": "8899f6ca-2241-db11-898a-0007e9e17ebd",
"HasChanged": null
}
},
"IsAuditEnabled": {
"Value": true,
"CanBeChanged": true,
"ManagedPropertyLogicalName": "canmodifyauditsettings"
},
"IsGlobalFilterEnabled": {
"Value": false,
"CanBeChanged": true,
"ManagedPropertyLogicalName": "canmodifyglobalfiltersettings"
},
"IsSortableEnabled": {
"Value": false,
"CanBeChanged": true,
"ManagedPropertyLogicalName": "canmodifyissortablesettings"
},
"IsCustomizable": {
"Value": true,
"CanBeChanged": false,
"ManagedPropertyLogicalName": "iscustomizable"
},
"IsRenameable": {
"Value": true,
"CanBeChanged": false,
"ManagedPropertyLogicalName": "isrenameable"
},
"IsValidForAdvancedFind": {
"Value": true,
"CanBeChanged": true,
"ManagedPropertyLogicalName": "canmodifysearchsettings"
},
"RequiredLevel": {
"Value": "None",
"CanBeChanged": true,
"ManagedPropertyLogicalName": "canmodifyrequirementlevelsettings"
},
"CanModifyAdditionalSettings": {
"Value": true,
"CanBeChanged": false,
"ManagedPropertyLogicalName": "canmodifyadditionalsettings"
}
}

您需要展开它们,按照您的查询应该是

https://mycompany.com/MyCompany/api/data/v9.1/EntityDefinitions(LogicalName='lead')/Attributes(LogicalName='statuscode')?$expand=OptionSet

结果将包含选项集值。

此方法适用于选项集(选择)、多选选项集(选择)、状态代码、状态代码和布尔字段。