문제

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.

  1. Sort an array in Power Automate in 3 easy steps
  2. How to implement Sort with Microsoft Flow in 3 actions within a loop
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top