Domanda

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?

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top