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

Time:05-28

Dynamics 365 v9.1.
How to get the non global optionset values metadata via REST API?

My query: https://mycompany.com/MyCompany/api/data/v9.1/EntityDefinitions(LogicalName='lead')/Attributes(LogicalName='statuscode')

My C# code:

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;
}

Below the result, but it doesn't contain OptionSet values metadata. How to get it?:

{
"@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"
}
}

CodePudding user response:

You need to expand them, following your query it should be

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

the result will contain the optionset values.

This approach works for optionsets (choice), multiselect optionsets (choices), statecode, statuscode and boolean fields.

  • Related