Hello!
I want to build a custom page using html/liquid/webapi to submit an entity data.
in the entity there is a choice field, and I need to build a dropdown for that field.
the question is how can I get all options of the choice defined in dataverse with liquid or webapi or any other way?
Thank you for your reply. @EmadBeshai
I think the approach of using power automate is a bit complicated.
In below link I found that all options are saved in 'stringmap' table.
I use fetchxml to query this table and get all options successfully in powerpages!
Hi @wangac ,
The above link will not work with CRM not portal.
To be able to retrieve the optionset values then you can use the power automate from power pages.
Please have a look to https://community.dynamics.com/blogs/post/?postid=8893c954-371f-47d5-8b04-70eec43d90fd
Also if you want to call power automate from power pages you can check this link https://www.inogic.com/blog/2023/07/how-to-use-power-automate-flow-in-power-pages/
If this post helps you with your problem, please mark this answer as Accepted Solution.
If you like my response, please give it a Thumbs Up.
Hi @anwarluck ,
Thanks!
Now I can retrieve the options in browser just use the url like below.
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
but when I use the url in javascript(ajax) code to retrieve the options in a page,
it failed.
Response Status Code: 401 Unauthorized
Is it possible to retrieve metadata by webapi in powerpages which use local authentication?
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
Hi @EmadBeshai ,
Thanks for your reply!
If I use power pages form component, the entity field are rendered as dropdown!
but I don't want to use form component,
I want to render the form myself, and use web api to post the form data.
my issue is I don't know how to get all options of the choice defined in dataverse to render the dropdown.
the choice is defined as below:
how can I get the all options of the choice use liquid or any other method?
Thanks!
Hi @wangac ,
What is your options type? are they optionset or entity rendered as dropdown?
By default the Power Pages will retrieve all the options, so could share more details about your issue?
If this post helps you with your problem, please mark this answer as Accepted Solution.
If you like my response, please give it a Thumbs Up.
I'm sorry.
Maybe I didn't make the problem clear.
my question isn't how to get all choice options of a html select tag.
[choice] in my question is dataverse table choice column.
my question is how to get all options of the choice defined in dataverse.
I'm sorry.
Maybe I didn't make the problem clear.
my question isn't how to get all choice options of a html select tag.
[choice] in my question is dataverse table choice column.
my question is how to get all options of the choice defined in dataverse.
Hi @wangac , in addition to the @EmadBeshai solution, you can use the below code to get all the options and apply your logic accordingly.
var selectElement = document.getElementById("mySelect");
// Get all the options
var options = selectElement.options;
for (var i = 0; i < options.length; i++) {
var option = options[i];
var value = option.value;
var text = option.text;
}
Hi @wangac ,
The below function is used to reorder the options inside the dropdown list you can use the same idea to retrieve the options and do what ever you want
function sortOptions(fieldname) {
var dorpdown = $("#"+fieldname);
dorpdown.html(dorpdown.find('option').sort(function (option1, option2) {
return $(option1).text() < $(option2).text() ? -1 : 1;
}));
}
If this post helps you with your problem, please mark your as accepted solution. If you like my response, please give it a thumbs up.
Fubar
69
Super User 2025 Season 1
oliver.rodrigues
49
Most Valuable Professional
Jon Unzueta
43