Hi @wangac, you can use the below code to retrieve the options using Webapi.
// Web API ajax wrapper
(function (webapi, $) {
function safeAjax(ajaxOptions) {
var deferredAjax = $.Deferred();
shell
.getTokenDeferred()
.done(function (token) {
// Add headers for ajax
if (!ajaxOptions.headers) {
$.extend(ajaxOptions, {
headers: {
__RequestVerificationToken: token
}
});
} else {
ajaxOptions.headers["__RequestVerificationToken"] = token;
}
$.ajax(ajaxOptions)
.done(function (data, textStatus, jqXHR) {
validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
})
.fail(deferredAjax.reject); // ajax
})
.fail(function () {
deferredAjax.rejectWith(this, arguments); // On token failure pass the token ajax and args
});
return deferredAjax.promise();
}
webapi.safeAjax = safeAjax;
})((window.webapi = window.webapi || {}), jQuery);
// Notification component
var notificationMsg = (function () {
var $processingMsgEl = $("#processingMsg"),
_msg = "Processing...",
_stack = 0,
_endTimeout;
return {
show: function (msg) {
$processingMsgEl.text(msg || _msg);
if (_stack === 0) {
clearTimeout(_endTimeout);
$processingMsgEl.show();
}
_stack++;
},
hide: function () {
_stack--;
if (_stack <= 0) {
_stack = 0;
clearTimeout(_endTimeout);
_endTimeout = setTimeout(function () {
$processingMsgEl.hide();
}, 500);
}
}
};
})();
// Applicaton ajax wrapper
function appAjax(processingMsg, ajaxOptions) {
notificationMsg.show(processingMsg);
return webapi
.safeAjax(ajaxOptions)
.fail(function (response) {
if (response.responseJSON) {
alert("Error: " + response.responseJSON.error.message);
} else {
alert("Error: Web API is not available... ");
}
})
.always(notificationMsg.hide);
}
function GetOptionSetDetails(crmURL, entityName, optionSetLogicalName) {
return appAjax("Loading...", {
type: "GET",
url: `https://${crmURL}/api/data/v9.2/EntityDefinitions(LogicalName='${entityName}')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$filter=LogicalName%20eq%20%27${optionSetLogicalName}%27&$expand=OptionSet`,
contentType: "application/json",
async: false,
success: function (data, status, xhr) {
var results = data;
for (var i = 0; i < results.value.length; i++) {
var result = results.value[i];
// Columns
const optionSet = result;
// Initialize an array to store the options
const options = [];
// Iterate through the "Options" array and extract the value and label
for (const optionData of optionSet.OptionSet.Options) {
const option = {
Value: optionData.Value,
Label: optionData.Label.LocalizedLabels[0].Label
};
options.push(option);
}
}
return options;
},
error: function (xhr, textStatus, errorThrown) {
console.log(xhr);
}
});
}
You will need to call the
GetOptionSetDetails(), providing the the following three parameters:
crmURL, entityName, optionSetLogicalName