REST API get choice values from column in ascending
-
08-02-2021 - |
문제
We are using this URI:
/_api/web/lists/getByTitle('MyListName')/fields/getbytitle('MyColumn')
to get the choice values from the column.
I need to pull them into an array alphabetically, but the $orderby.asc
is not working.
Is there a way to get this collection back in alpha ascending order?
해결책
As per my understanding there is no direct way to get the choices from choice field in alphabetical order using SharePoint REST API (correct me If I am wrong).
But you can sort the choices after you got the result from REST API using JavaScript. Use sort()
method like below in success function:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
Reference: JavaScript Array sort() Method.
This is the code I have used before to achieve the same:
$.ajax({
url: <siteUrl> + "/_api/web/lists/GetByTitle('List Name')/fields?$filter=EntityPropertyName eq 'Choice Field Internal Name'",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
var choicesArray = [];
if(data && data.d.results) {
choicesArray = data.d.results[0].Choices.results.sort();
}
},
error: function (error) {
alert(JSON.stringify(error));
}
});
Update:
You can also sort the array in Microsoft Flow/Power automate. Please check below links for possible solution.
제휴하지 않습니다 sharepoint.stackexchange