Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - Customize & Extend
Answered

How to get all options of a choice with liquid or webapi?

(0) ShareShare
ReportReport
Posted on by 38

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?

Categories:
  • Verified answer
    wangac Profile Picture
    38 on at
    Re: How to get all options of a choice with liquid or webapi?

    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!

    https://powerusers.microsoft.com/t5/Customize-Extend/Get-Avaialble-options-from-an-Option-Set-in-Power-Pages-Portal/td-p/1913488

     

  • EmadBeshai Profile Picture
    806 Super User 2025 Season 1 on at
    Re: How to get all options of a choice with liquid or webapi?

    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.

  • wangac Profile Picture
    38 on at
    Re: How to get all options of a choice with liquid or webapi?

    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?

  • Anwarluck Profile Picture
    113 on at
    Re: How to get all options of a choice with liquid or webapi?

    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
  • wangac Profile Picture
    38 on at
    Re: How to get all options of a choice?

    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:

    wangac_0-1698021644347.png

     

    how can I get the all options of the choice use liquid or any other method?

     

    Thanks!

  • EmadBeshai Profile Picture
    806 Super User 2025 Season 1 on at
    Re: How to get all options of a choice?

    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.

  • wangac Profile Picture
    38 on at
    Re: How to get all options of a choice?

    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.

     

  • wangac Profile Picture
    38 on at
    Re: How to get all options of a choice?

    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.

     

  • Anwarluck Profile Picture
    113 on at
    Re: How to get all options of a choice?

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

     

  • EmadBeshai Profile Picture
    806 Super User 2025 Season 1 on at
    Re: How to get all options of a choice?

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 69 Super User 2025 Season 1

#2
oliver.rodrigues Profile Picture

oliver.rodrigues 49 Most Valuable Professional

#3
Jon Unzueta Profile Picture

Jon Unzueta 43

Featured topics